Exception: BaseCradle::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/basecradle/errors.rb,
lib/basecradle/errors.rb

Overview

Root of every exception this SDK raises. Rescue BaseCradle::Error to catch everything.

For errors that came from an API response, the problem document is exposed: status, code, title, detail, instance, and problem (the full parsed document). For errors that never reached the API (missing token, connection failure), those are nil.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, status: nil, code: nil, title: nil, detail: nil, instance: nil, problem: nil) ⇒ Error

Returns a new instance of Error.



14
15
16
17
18
19
20
21
22
23
# File 'lib/basecradle/errors.rb', line 14

def initialize(message = nil, status: nil, code: nil, title: nil, detail: nil, instance: nil,
               problem: nil)
  super(message)
  @status = status
  @code = code
  @title = title
  @detail = detail
  @instance = instance
  @problem = problem
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



12
13
14
# File 'lib/basecradle/errors.rb', line 12

def code
  @code
end

#detailObject (readonly)

Returns the value of attribute detail.



12
13
14
# File 'lib/basecradle/errors.rb', line 12

def detail
  @detail
end

#instanceObject (readonly)

Returns the value of attribute instance.



12
13
14
# File 'lib/basecradle/errors.rb', line 12

def instance
  @instance
end

#problemObject (readonly)

Returns the value of attribute problem.



12
13
14
# File 'lib/basecradle/errors.rb', line 12

def problem
  @problem
end

#statusObject (readonly)

Returns the value of attribute status.



12
13
14
# File 'lib/basecradle/errors.rb', line 12

def status
  @status
end

#titleObject (readonly)

Returns the value of attribute title.



12
13
14
# File 'lib/basecradle/errors.rb', line 12

def title
  @title
end

Class Method Details

.from_response(status:, problem:, retry_after: nil) ⇒ Object

Build the right exception for a non-2xx API response.

problem is the parsed problem+json document (a Hash) or nil. Unknown codes fall back to BaseCradle::Error (the API is additive-only; a new error code must never crash the SDK), and non-problem+json bodies produce a bare Error carrying just the HTTP status.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/basecradle/errors.rb', line 154

def self.from_response(status:, problem:, retry_after: nil)
  unless problem.is_a?(Hash) && problem.key?("code")
    return new("API request failed with HTTP #{status}", status: status,
                                                          problem: problem.is_a?(Hash) ? problem : nil)
  end

  code = problem["code"]
  detail = problem["detail"]
  title = problem["title"]
  message = detail || title || "API request failed with HTTP #{status}"
  common = {
    status: problem.fetch("status", status),
    code: code,
    title: title,
    detail: detail,
    instance: problem["instance"],
    problem: problem
  }

  error_class = CODE_TO_ERROR.fetch(code, self)

  if error_class <= ValidationError
    error_class.new(message, errors: problem["errors"], **common)
  elsif error_class <= RateLimitedError
    error_class.new(message, retry_after: retry_after, **common)
  else
    error_class.new(message, **common)
  end
end