Exception: Lara::LaraApiError
- Defined in:
- lib/lara/errors.rb
Overview
API error with HTTP status, type and message.
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Attributes inherited from LaraError
Class Method Summary collapse
-
.from_response(response) ⇒ Object
Builds an error from an HTTP response with JSON body.
Instance Method Summary collapse
-
#initialize(status_code, type, message) ⇒ LaraApiError
constructor
A new instance of LaraApiError.
Constructor Details
#initialize(status_code, type, message) ⇒ LaraApiError
Returns a new instance of LaraApiError.
38 39 40 41 42 |
# File 'lib/lara/errors.rb', line 38 def initialize(status_code, type, ) super("(HTTP #{status_code}) #{type}: #{}", status_code) @type = type @message = end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
18 19 20 |
# File 'lib/lara/errors.rb', line 18 def type @type end |
Class Method Details
.from_response(response) ⇒ Object
Builds an error from an HTTP response with JSON body. Supports both { “error”: { “type”: “…”, “message”: “…” } } and { “type”: “…”, “message”: “…” } response formats.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/lara/errors.rb', line 23 def self.from_response(response) data = begin JSON.parse(response.body) rescue StandardError {} end error = data["error"] || data error_type = error["type"] || "UnknownError" = error["message"] || "An unknown error occurred" new(response.status, error_type, ) end |