Exception: Billrb::ApiError

Inherits:
Error
  • Object
show all
Defined in:
lib/billrb/errors.rb

Overview

Raised when the API responds with a non-2xx status. Subclasses map common statuses; ‘status` and `body` carry the raw response for callers that need the BILL error code or details.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, status: nil, body: nil) ⇒ ApiError

Returns a new instance of ApiError.



19
20
21
22
23
# File 'lib/billrb/errors.rb', line 19

def initialize(message, status: nil, body: nil)
  super(message)
  @status = status
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



17
18
19
# File 'lib/billrb/errors.rb', line 17

def body
  @body
end

#statusObject (readonly)

Returns the value of attribute status.



17
18
19
# File 'lib/billrb/errors.rb', line 17

def status
  @status
end

Class Method Details

.from_response(status:, body:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/billrb/errors.rb', line 25

def self.from_response(status:, body:)
  klass = case status
    when 400 then BadRequestError
    when 401 then AuthenticationError
    when 403 then ForbiddenError
    when 404 then NotFoundError
    when 429 then RateLimitError
    when 500..599 then ServerError
    else ApiError
    end
  klass.new(message_from(body) || "HTTP #{status}", status: status, body: body)
end