Exception: Posthaste::Error

Inherits:
StandardError
  • Object
show all
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.

Instance Attribute Summary collapse

Instance Method Summary collapse

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(message, 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(message, api_key))
  @type = type
  @status = status
  @body = body
  @request_id = request_id
  @retry_after_seconds = retry_after_seconds
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



45
46
47
# File 'lib/posthaste/errors.rb', line 45

def body
  @body
end

#request_idObject (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_secondsObject (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

#statusObject (readonly)

Returns the value of attribute status.



45
46
47
# File 'lib/posthaste/errors.rb', line 45

def status
  @status
end

#typeObject (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

#inspectObject



83
84
85
86
# File 'lib/posthaste/errors.rb', line 83

def inspect
  "#<#{self.class.name} type=#{type.inspect} status=#{status} " \
    "message=#{message.inspect}>"
end

#quota_exhausted?Boolean

Returns:

  • (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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


74
75
76
# File 'lib/posthaste/errors.rb', line 74

def transient?
  rate_limited? || status == 408 || status == 429 || status >= 500
end