Exception: Forem::ForemError
- Inherits:
-
StandardError
- Object
- StandardError
- Forem::ForemError
- Defined in:
- lib/forem/errors.rb
Overview
Base error class for all errors raised by the forem-ruby library.
Every error exposes the raw HTTP context (status code, body, headers) so callers can inspect the upstream response without re-issuing the request.
Direct Known Subclasses
APIConnectionError, APIError, AuthenticationError, AuthorizationError, ConflictError, InvalidRequestError, NotFoundError, RateLimitError
Instance Attribute Summary collapse
-
#code ⇒ String?
readonly
An optional machine-readable error code extracted from the API response body, or
nil. -
#http_body ⇒ String?
readonly
The raw HTTP response body as a string, or
nil. -
#http_headers ⇒ Hash?
readonly
A hash of HTTP response headers, or
nil. -
#http_status ⇒ Integer?
readonly
The HTTP status code returned by the server (e.g.
401,404,429), ornilif no HTTP response was received.
Instance Method Summary collapse
-
#initialize(message = nil, http_status: nil, http_body: nil, http_headers: nil, code: nil) ⇒ ForemError
constructor
Initialize a new ForemError.
-
#parsed_body ⇒ Hash, ...
The response body parsed as JSON.
Constructor Details
#initialize(message = nil, http_status: nil, http_body: nil, http_headers: nil, code: nil) ⇒ ForemError
Initialize a new ForemError.
43 44 45 46 47 48 49 |
# File 'lib/forem/errors.rb', line 43 def initialize( = nil, http_status: nil, http_body: nil, http_headers: nil, code: nil) @http_status = http_status @http_body = http_body @http_headers = http_headers @code = code super() end |
Instance Attribute Details
#code ⇒ String? (readonly)
Returns an optional machine-readable error code extracted
from the API response body, or nil.
33 34 35 |
# File 'lib/forem/errors.rb', line 33 def code @code end |
#http_body ⇒ String? (readonly)
Returns the raw HTTP response body as a string, or nil.
26 27 28 |
# File 'lib/forem/errors.rb', line 26 def http_body @http_body end |
#http_headers ⇒ Hash? (readonly)
Returns a hash of HTTP response headers, or nil.
29 30 31 |
# File 'lib/forem/errors.rb', line 29 def http_headers @http_headers end |
#http_status ⇒ Integer? (readonly)
Returns the HTTP status code returned by the server
(e.g. 401, 404, 429), or nil if no HTTP response was received.
23 24 25 |
# File 'lib/forem/errors.rb', line 23 def http_status @http_status end |
Instance Method Details
#parsed_body ⇒ Hash, ...
The response body parsed as JSON.
Useful when the API returns structured detail alongside an error. An empty
or unparseable body yields nil.
65 66 67 68 69 70 71 |
# File 'lib/forem/errors.rb', line 65 def parsed_body return @parsed_body if defined?(@parsed_body) @parsed_body = (JSON.parse(http_body) if http_body && !http_body.empty?) rescue JSON::ParserError @parsed_body = nil end |