Exception: Backlex::Error
- Inherits:
-
StandardError
- Object
- StandardError
- Backlex::Error
- Defined in:
- lib/backlex/error.rb
Overview
A non-2xx response from the backlex API (or a transport failure), mirroring the TS SDK’s BacklexError. The API returns errors as ‘{ “error”: { “code”, “message”, “details”? } }`; callers branch on #status / #code rather than parsing strings.
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#details ⇒ Object
readonly
Returns the value of attribute details.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
-
.from(status, body) ⇒ Object
Parse the ‘{ “error”: … }` envelope from a response body.
Instance Method Summary collapse
-
#initialize(status, code, message, details = nil) ⇒ Error
constructor
A new instance of Error.
Constructor Details
#initialize(status, code, message, details = nil) ⇒ Error
Returns a new instance of Error.
11 12 13 14 15 16 |
# File 'lib/backlex/error.rb', line 11 def initialize(status, code, , details = nil) super() @status = status @code = code @details = details end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
9 10 11 |
# File 'lib/backlex/error.rb', line 9 def code @code end |
#details ⇒ Object (readonly)
Returns the value of attribute details.
9 10 11 |
# File 'lib/backlex/error.rb', line 9 def details @details end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
9 10 11 |
# File 'lib/backlex/error.rb', line 9 def status @status end |
Class Method Details
.from(status, body) ⇒ Object
Parse the ‘{ “error”: … }` envelope from a response body.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/backlex/error.rb', line 19 def self.from(status, body) code = "UNKNOWN" = "HTTP #{status}" details = nil unless body.nil? || body.empty? begin env = JSON.parse(body) err = env["error"] if err.is_a?(Hash) code = err["code"] if err["code"] = err["message"] if err["message"] details = err["details"] end rescue JSON::ParserError # non-JSON error body — keep the generic message end end new(status, code, , details) end |