Module: Blueticks::Errors

Defined in:
lib/blueticks/errors.rb

Defined Under Namespace

Classes: APIConnectionError, APIError, AuthenticationError, BadRequestError, BluetickError, NotFoundError, PermissionDeniedError, RateLimitError, ValidationError, WebhookVerificationError

Constant Summary collapse

STATUS_TO_CLASS =
{
  400 => BadRequestError,
  401 => AuthenticationError,
  403 => PermissionDeniedError,
  404 => NotFoundError,
  422 => BadRequestError,
  429 => RateLimitError
}.freeze

Class Method Summary collapse

Class Method Details

.from_envelope(status_code:, body:, response: nil, retry_after: nil) ⇒ Object

Map a non-2xx response body to a typed Blueticks exception.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/blueticks/errors.rb', line 79

def from_envelope(status_code:, body:, response: nil, retry_after: nil)
  cls = STATUS_TO_CLASS.fetch(status_code, APIError)
  err = body.is_a?(Hash) ? body["error"] : nil

  if err.is_a?(Hash) && err["code"].is_a?(String)
    kwargs = {
      status_code: status_code,
      code: err["code"],
      message: err["message"].to_s,
      request_id: err["request_id"],
      response: response
    }
    kwargs[:retry_after] = retry_after if cls == RateLimitError
    return cls.new(**kwargs)
  end

  raw = body.is_a?(String) ? body : body.inspect
  truncated = raw.length > 200 ? "#{raw[0, 200]}..." : raw
  kwargs = {
    status_code: status_code,
    code: nil,
    message: truncated,
    request_id: nil,
    response: response
  }
  kwargs[:retry_after] = retry_after if cls == RateLimitError
  cls.new(**kwargs)
end