Exception: Llmshim::APIError

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

Class Method Summary collapse

Instance Method Summary collapse

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

Instance Attribute Details

#bodyObject (readonly)

Raw response body (String).



18
19
20
# File 'lib/llmshim/errors.rb', line 18

def body
  @body
end

#codeObject (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

#statusObject (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
  message = nil
  begin
    parsed = JSON.parse(body.to_s)
    err = parsed["error"]
    if err.is_a?(Hash)
      code = err["code"]
      message = err["message"]
    end
  rescue JSON::ParserError
    # fall through to raw body
  end
  message ||= (body.to_s.empty? ? "HTTP #{status}" : body.to_s)
  new(message, status: status, code: code, body: body)
end