Module: ShipEngine::Exceptions

Defined in:
lib/shipengine/exceptions.rb,
lib/shipengine/exceptions/error_code.rb,
lib/shipengine/exceptions/error_type.rb

Defined Under Namespace

Classes: AccountStatusError, BusinessRulesError, ErrorCode, ErrorType, RateLimitError, SecurityError, ShipEngineError, SystemError, TimeoutError, ValidationError

Constant Summary collapse

DEFAULT_SOURCE =
'shipengine'

Class Method Summary collapse

Class Method Details

.create_error_instance(type:, message:, code:, request_id: nil, source: nil, config: nil) ⇒ Object

rubocop:todo Metrics/ParameterLists



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/shipengine/exceptions.rb', line 105

def self.create_error_instance(type:, message:, code:, request_id: nil, source: nil, config: nil) # rubocop:todo Metrics/ParameterLists
  case type
  when Exceptions::ErrorType.get(:BUSINESS_RULES)
    BusinessRulesError.new(message:, code:, request_id:, source:)
  when Exceptions::ErrorType.get(:VALIDATION)
    ValidationError.new(message:, code:, request_id:, source:)
  when Exceptions::ErrorType.get(:ACCOUNT_STATUS)
    AccountStatusError.new(message:, code:, request_id:, source:)
  when Exceptions::ErrorType.get(:SECURITY)
    SecurityError.new(message:, code:, request_id:, source:)
  when Exceptions::ErrorType.get(:SYSTEM)
    case code
    when ErrorCode.get(:RATE_LIMIT_EXCEEDED)
      RateLimitError.new(message:, request_id:, source:, retries: config.retries)
    when ErrorCode.get(:TIMEOUT)
      TimeoutError.new(message:, request_id:, source:)
    else
      SystemError.new(message:, code:, request_id:, source:)
    end
  else
    ShipEngineError.new(message:, code:, request_id:, source:)
  end
end

.create_invalid_field_value_error(message, request_id = nil, source = nil) ⇒ Object

only create custom errors for error “types” (which encompass codes). Prefer to use generic ShipEngine errors.



33
34
35
# File 'lib/shipengine/exceptions.rb', line 33

def self.create_invalid_field_value_error(message, request_id = nil, source = nil)
  ValidationError.new(message:, code: Exceptions::ErrorCode.get(:INVALID_FIELD_VALUE), request_id:, source:)
end

.create_invariant_error(message, request_id = nil) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/shipengine/exceptions.rb', line 46

def self.create_invariant_error(message, request_id = nil)
  SystemError.new(
    message: "INVARIANT ERROR: #{message}",
    code: Exceptions::ErrorCode.get(:UNSPECIFIED),
    request_id:
  )
end

.create_required_error(field_name, request_id = nil, source = nil) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/shipengine/exceptions.rb', line 37

def self.create_required_error(field_name, request_id = nil, source = nil)
  ValidationError.new(
    message: "#{field_name} must be specified.",
    code: Exceptions::ErrorCode.get(:FIELD_VALUE_REQUIRED),
    request_id:,
    source:
  )
end