Exception: Backlex::Error

Inherits:
StandardError
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, message, details = nil)
  super(message)
  @status = status
  @code = code
  @details = details
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



9
10
11
# File 'lib/backlex/error.rb', line 9

def code
  @code
end

#detailsObject (readonly)

Returns the value of attribute details.



9
10
11
# File 'lib/backlex/error.rb', line 9

def details
  @details
end

#statusObject (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"
  message = "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"]
        message = 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, message, details)
end