Exception: Llmshim::APIError
- Defined in:
- lib/llmshim/errors.rb
Overview
Raised when the proxy returns a non-2xx response.
The proxy encodes errors as an ErrorResponse object
(+{ "error": { "code": ..., "message": ... } }+). When the body cannot
be parsed, code falls back to nil and message to the raw body.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Raw response body (String).
-
#code ⇒ Object
readonly
Machine-readable error code from the proxy (String or nil).
-
#status ⇒ Object
readonly
HTTP status code of the failed response (Integer).
Class Method Summary collapse
-
.from_response(status, body) ⇒ Object
Build an APIError from an HTTP status and response body.
Instance Method Summary collapse
-
#initialize(message, status:, code: nil, body: nil) ⇒ APIError
constructor
A new instance of APIError.
Constructor Details
#initialize(message, status:, code: nil, body: nil) ⇒ APIError
Returns a new instance of APIError.
20 21 22 23 24 25 |
# File 'lib/llmshim/errors.rb', line 20 def initialize(, status:, code: nil, body: nil) @status = status @code = code @body = body super() end |
Instance Attribute Details
#body ⇒ Object (readonly)
Raw response body (String).
18 19 20 |
# File 'lib/llmshim/errors.rb', line 18 def body @body end |
#code ⇒ Object (readonly)
Machine-readable error code from the proxy (String or nil).
16 17 18 |
# File 'lib/llmshim/errors.rb', line 16 def code @code end |
#status ⇒ Object (readonly)
HTTP status code of the failed response (Integer).
14 15 16 |
# File 'lib/llmshim/errors.rb', line 14 def status @status end |
Class Method Details
.from_response(status, body) ⇒ Object
Build an APIError from an HTTP status and response body.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/llmshim/errors.rb', line 28 def self.from_response(status, body) code = nil = nil begin parsed = JSON.parse(body.to_s) err = parsed["error"] if err.is_a?(Hash) code = err["code"] = err["message"] end rescue JSON::ParserError # fall through to raw body end ||= (body.to_s.empty? ? "HTTP #{status}" : body.to_s) new(, status: status, code: code, body: body) end |