Exception: Riveter::APIError
- Defined in:
- lib/riveter/errors.rb
Overview
A non-2xx HTTP response, normalized across both error body dialects.
Constant Summary collapse
- FALLBACK_TYPES =
{ 400 => "bad_request", 401 => "unauthorized", 403 => "forbidden", 404 => "not_found", 409 => "conflict", 422 => "validation", 429 => "rate_limited" }.freeze
Instance Attribute Summary collapse
-
#details ⇒ Object
readonly
Returns the value of attribute details.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.from_response(status, body) ⇒ Object
Builds an APIError from either the clean { "error" => ... } envelope or the legacy { "request_status" => "error" } shape (401s).
Instance Method Summary collapse
-
#initialize(status:, type:, message:, details: nil) ⇒ APIError
constructor
A new instance of APIError.
Constructor Details
#initialize(status:, type:, message:, details: nil) ⇒ APIError
Returns a new instance of APIError.
27 28 29 30 31 32 |
# File 'lib/riveter/errors.rb', line 27 def initialize(status:, type:, message:, details: nil) super() @status = status @type = type @details = details end |
Instance Attribute Details
#details ⇒ Object (readonly)
Returns the value of attribute details.
25 26 27 |
# File 'lib/riveter/errors.rb', line 25 def details @details end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
25 26 27 |
# File 'lib/riveter/errors.rb', line 25 def status @status end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
25 26 27 |
# File 'lib/riveter/errors.rb', line 25 def type @type end |
Class Method Details
.from_response(status, body) ⇒ Object
Builds an APIError from either the clean { "error" => ... } envelope or the legacy { "request_status" => "error" } shape (401s).
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/riveter/errors.rb', line 36 def self.from_response(status, body) if body.is_a?(Hash) error = body["error"] if error.is_a?(Hash) && error["type"].is_a?(String) return new( status: status, type: error["type"], message: error["message"] || "HTTP #{status}", details: error["details"] ) end if body["request_status"] == "error" return new( status: status, type: body["error_type"] || "error", message: body["message"] || "HTTP #{status}", details: body["errors"] ) end end = body.is_a?(String) && !body.empty? ? body[0, 200] : "HTTP #{status}" new(status: status, type: FALLBACK_TYPES[status] || "http_#{status}", message: ) end |