Exception: Posthaste::Error
- Inherits:
-
StandardError
- Object
- StandardError
- Posthaste::Error
- Defined in:
- lib/posthaste/errors.rb
Overview
The root of everything this gem raises.
Rescue Posthaste::Error to catch every refusal, or one of the subclasses
below to catch a KIND of refusal. Branch on #type, never on #message:
the type is the stable contract and the sentence is not.
Direct Known Subclasses
APIStatusError, ConfigurationError, ConnectionError, MappingError
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#request_id ⇒ Object
readonly
Returns the value of attribute request_id.
-
#retry_after_seconds ⇒ Object
readonly
Returns the value of attribute retry_after_seconds.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #error_field(name) ⇒ Object
-
#initialize(message, type: 'unknown_error', status: 0, body: nil, request_id: nil, retry_after_seconds: nil, api_key: nil) ⇒ Error
constructor
A new instance of Error.
- #inspect ⇒ Object
- #quota_exhausted? ⇒ Boolean
-
#rate_limited? ⇒ Boolean
The four 429s mean four different things and nothing about the status says which.
-
#transient? ⇒ Boolean
Worth repeating the same request later.
Constructor Details
#initialize(message, type: 'unknown_error', status: 0, body: nil, request_id: nil, retry_after_seconds: nil, api_key: nil) ⇒ Error
Returns a new instance of Error.
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/posthaste/errors.rb', line 47 def initialize(, type: 'unknown_error', status: 0, body: nil, request_id: nil, retry_after_seconds: nil, api_key: nil) # Redacted at CONSTRUCTION, not at print time. An exception message is # copied into logs, error reporters and tickets by machinery this gem # never sees, so the only safe moment is before the string exists. super(Redaction.redact(, api_key)) @type = type @status = status @body = body @request_id = request_id @retry_after_seconds = retry_after_seconds end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
45 46 47 |
# File 'lib/posthaste/errors.rb', line 45 def body @body end |
#request_id ⇒ Object (readonly)
Returns the value of attribute request_id.
45 46 47 |
# File 'lib/posthaste/errors.rb', line 45 def request_id @request_id end |
#retry_after_seconds ⇒ Object (readonly)
Returns the value of attribute retry_after_seconds.
45 46 47 |
# File 'lib/posthaste/errors.rb', line 45 def retry_after_seconds @retry_after_seconds end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
45 46 47 |
# File 'lib/posthaste/errors.rb', line 45 def status @status end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
45 46 47 |
# File 'lib/posthaste/errors.rb', line 45 def type @type end |
Instance Method Details
#error_field(name) ⇒ Object
78 79 80 81 |
# File 'lib/posthaste/errors.rb', line 78 def error_field(name) envelope = body.is_a?(Hash) ? body['error'] : nil envelope.is_a?(Hash) ? envelope[name.to_s] : nil end |
#inspect ⇒ Object
83 84 85 86 |
# File 'lib/posthaste/errors.rb', line 83 def inspect "#<#{self.class.name} type=#{type.inspect} status=#{status} " \ "message=#{.inspect}>" end |
#quota_exhausted? ⇒ Boolean
67 68 69 |
# File 'lib/posthaste/errors.rb', line 67 def quota_exhausted? type == 'daily_limit_reached' || type == 'monthly_limit_reached' end |
#rate_limited? ⇒ Boolean
The four 429s mean four different things and nothing about the status says which. These two predicates are the difference between waiting a few seconds and hammering a wall until the calendar moves.
63 64 65 |
# File 'lib/posthaste/errors.rb', line 63 def rate_limited? type == 'rate_limited' || type == 'platform_paused' end |
#transient? ⇒ Boolean
Worth repeating the same request later. NOT a promise that repeating it is safe — that depends on whether the request can duplicate an effect, which is what an idempotency key settles.
74 75 76 |
# File 'lib/posthaste/errors.rb', line 74 def transient? rate_limited? || status == 408 || status == 429 || status >= 500 end |