Exception: BaseCradle::Error
- Inherits:
-
StandardError
- Object
- StandardError
- BaseCradle::Error
- 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.
Direct Known Subclasses
APIConnectionError, AccountSuspendedError, AuthenticationError, EndpointDisabledError, ForbiddenError, InvalidRequestError, MissingFieldError, MissingTokenError, NotFoundError, PayloadTooLargeError, RateLimitedError, ValidationError
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#detail ⇒ Object
readonly
Returns the value of attribute detail.
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#problem ⇒ Object
readonly
Returns the value of attribute problem.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
-
.from_response(status:, problem:, retry_after: nil) ⇒ Object
Build the right exception for a non-2xx API response.
Instance Method Summary collapse
-
#initialize(message = nil, status: nil, code: nil, title: nil, detail: nil, instance: nil, problem: nil) ⇒ Error
constructor
A new instance of Error.
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( = nil, status: nil, code: nil, title: nil, detail: nil, instance: nil, problem: nil) super() @status = status @code = code @title = title @detail = detail @instance = instance @problem = problem end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
12 13 14 |
# File 'lib/basecradle/errors.rb', line 12 def code @code end |
#detail ⇒ Object (readonly)
Returns the value of attribute detail.
12 13 14 |
# File 'lib/basecradle/errors.rb', line 12 def detail @detail end |
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
12 13 14 |
# File 'lib/basecradle/errors.rb', line 12 def instance @instance end |
#problem ⇒ Object (readonly)
Returns the value of attribute problem.
12 13 14 |
# File 'lib/basecradle/errors.rb', line 12 def problem @problem end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
12 13 14 |
# File 'lib/basecradle/errors.rb', line 12 def status @status end |
#title ⇒ Object (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"] = 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(, errors: problem["errors"], **common) elsif error_class <= RateLimitedError error_class.new(, retry_after: retry_after, **common) else error_class.new(, **common) end end |