Exception: Assinafy::ApiError
- Defined in:
- lib/assinafy/errors.rb,
sig/assinafy.rbs
Overview
Raised when the API responds with a non-2xx status, or with a 2xx response envelope whose embedded status code indicates failure.
The Assinafy v1 API returns two distinct error-body shapes, both handled here:
- Framework errors:
{"name":"Not Found","message":"Página não encontrada.","code":0,"status":404} - Application envelopes:
{"status":404,"data":null,"message":"Template não encontrado."}
Instance Attribute Summary collapse
-
#error_code ⇒ Integer, ...
readonly
The API's
codefield, when present. -
#error_name ⇒ String?
readonly
The API's
namefield (framework errors only). -
#response_data ⇒ Hash, ...
readonly
Raw response body.
-
#status_code ⇒ Integer
readonly
HTTP-style status code reported by the API.
Attributes inherited from Error
Class Method Summary collapse
-
.from_response(status_code, response_data) ⇒ ApiError
Build an ApiError from an HTTP status and parsed body.
Instance Method Summary collapse
-
#initialize(message, status_code, response_data = nil) ⇒ ApiError
constructor
A new instance of ApiError.
Constructor Details
#initialize(message, status_code, response_data = nil) ⇒ ApiError
Returns a new instance of ApiError.
45 46 47 48 49 50 51 52 53 |
# File 'lib/assinafy/errors.rb', line 45 def initialize(, status_code, response_data = nil) super(, { status_code: status_code, response_data: response_data }) @status_code = status_code @response_data = response_data return unless response_data.is_a?(Hash) @error_name = response_data['name'] @error_code = response_data['code'] end |
Instance Attribute Details
#error_code ⇒ Integer, ... (readonly)
Returns the API's code field, when present.
43 44 45 |
# File 'lib/assinafy/errors.rb', line 43 def error_code @error_code end |
#error_name ⇒ String? (readonly)
Returns the API's name field (framework errors only).
41 42 43 |
# File 'lib/assinafy/errors.rb', line 41 def error_name @error_name end |
#response_data ⇒ Hash, ... (readonly)
Returns raw response body.
39 40 41 |
# File 'lib/assinafy/errors.rb', line 39 def response_data @response_data end |
#status_code ⇒ Integer (readonly)
Returns HTTP-style status code reported by the API.
37 38 39 |
# File 'lib/assinafy/errors.rb', line 37 def status_code @status_code end |
Class Method Details
.from_response(status_code, response_data) ⇒ ApiError
Build an Assinafy::ApiError from an HTTP status and parsed body. Reads the human
message from message, error, or name (in that order).
61 62 63 64 65 |
# File 'lib/assinafy/errors.rb', line 61 def self.from_response(status_code, response_data) data = response_data.is_a?(Hash) ? response_data : {} = data['message'] || data['error'] || data['name'] || 'API request failed' new(.to_s, status_code, response_data) end |