Exception: HttpResource::ApiError
- Defined in:
- lib/http_resource/errors.rb
Overview
Raised on a non-2xx response or a transport failure. Carries the HTTP status (an Integer, or nil for transport failures) + the raw body, so a background worker can branch its retry: drop on a 4xx (client_error?), retry on a 5xx (server_error?) or a transport failure (TransportError, status nil).
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
-
.for_status(message, status:, body:) ⇒ Object
Map an HTTP status to the most specific ApiError subclass.
Instance Method Summary collapse
- #client_error? ⇒ Boolean
-
#initialize(message = nil, status: nil, body: nil) ⇒ ApiError
constructor
A new instance of ApiError.
- #not_found? ⇒ Boolean
- #server_error? ⇒ Boolean
Constructor Details
#initialize(message = nil, status: nil, body: nil) ⇒ ApiError
Returns a new instance of ApiError.
24 25 26 27 28 |
# File 'lib/http_resource/errors.rb', line 24 def initialize( = nil, status: nil, body: nil) @status = status @body = body super( || "HTTP error (status=#{status.inspect})") end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
22 23 24 |
# File 'lib/http_resource/errors.rb', line 22 def body @body end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
22 23 24 |
# File 'lib/http_resource/errors.rb', line 22 def status @status end |
Class Method Details
.for_status(message, status:, body:) ⇒ Object
Map an HTTP status to the most specific ApiError subclass.
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/http_resource/errors.rb', line 43 def self.for_status(, status:, body:) klass = case status when 404 then NotFoundError when 422 then ValidationError when 401, 403 then AuthError when 400..499 then ClientError when 300..399 then RedirectError when 500..599 then ServerError else self end klass.new(, status:, body:) end |
Instance Method Details
#client_error? ⇒ Boolean
30 31 32 |
# File 'lib/http_resource/errors.rb', line 30 def client_error? status.is_a?(Integer) && status.between?(400, 499) end |
#not_found? ⇒ Boolean
38 39 40 |
# File 'lib/http_resource/errors.rb', line 38 def not_found? status == 404 end |
#server_error? ⇒ Boolean
34 35 36 |
# File 'lib/http_resource/errors.rb', line 34 def server_error? status.is_a?(Integer) && status.between?(500, 599) end |