Exception: Api::Datadive::Error

Inherits:
StandardError
  • Object
show all
Defined in:
app/services/api/datadive/error.rb

Overview

Error hierarchy for the DataDive client.

Mirrors the HTTP-status → class mapping used by Taxjar::Error so
callers can rescue a specific failure mode (e.g. +Unauthorized+ for a
bad/expired key, +TooManyRequests+ for rate limiting) or the base
Error for any API failure. Transport / parse failures
are wrapped as ServerError so retry paths still fire.

See Also:

Defined Under Namespace

Classes: BadRequest, Forbidden, GatewayTimeout, InternalServerError, NotFound, ServiceUnavailable, TooManyRequests, Unauthorized, UnprocessableEntity

Constant Summary collapse

ClientError =
Class.new(self)
ServerError =
Class.new(self)
STATUS_MAP =

HTTP status → error class. Statuses outside the map fall back to
ServerError (>= 500) or ClientError. Matches the codes DataDive
documents for the v1 endpoints (400/401/403/404/429/500).

{
  400 => BadRequest,
  401 => Unauthorized,
  403 => Forbidden,
  404 => NotFound,
  422 => UnprocessableEntity,
  429 => TooManyRequests,
  500 => InternalServerError,
  503 => ServiceUnavailable,
  504 => GatewayTimeout
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = '', code = nil) ⇒ Error

Returns a new instance of Error.

Parameters:

  • message (String) (defaults to: '')
  • code (Integer, nil) (defaults to: nil)

    HTTP status



66
67
68
69
# File 'app/services/api/datadive/error.rb', line 66

def initialize(message = '', code = nil)
  super(message)
  @code = code
end

Instance Attribute Details

#codeInteger? (readonly)

Returns the HTTP status, or nil for transport errors.

Returns:

  • (Integer, nil)

    the HTTP status, or nil for transport errors



16
17
18
# File 'app/services/api/datadive/error.rb', line 16

def code
  @code
end

Class Method Details

.from_response(status, body) ⇒ Api::Datadive::Error

Build the appropriate error for a non-2xx response. The message is
taken from DataDive's JSON message (falling back to detail /
error), then a generic "DataDive HTTP ".

Parameters:

  • status (Integer)

    HTTP status code

  • body (Hash, Object)

    parsed response body

Returns:



57
58
59
60
61
# File 'app/services/api/datadive/error.rb', line 57

def from_response(status, body)
  klass  = STATUS_MAP[status] || (status.to_i >= 500 ? ServerError : ClientError)
  detail = body['message'] || body['detail'] || body['error'] if body.is_a?(Hash)
  klass.new(detail.presence || "DataDive HTTP #{status}", status)
end