Exception: Api::Taxjar::Error

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

Overview

Error hierarchy for the direct TaxJar client.

Mirrors the HTTP-status → class mapping that the taxjar-ruby gem
exposed (Taxjar::Error::NotFound, …::UnprocessableEntity,
…::ServerError, …::Forbidden, …) so the existing rescue clauses in
the submit/sync services keep working unchanged after the port.

See Also:

Defined Under Namespace

Classes: BadRequest, Forbidden, GatewayTimeout, Gone, InternalServerError, MethodNotAllowed, NotAcceptable, 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.

{
  400 => BadRequest,
  401 => Unauthorized,
  403 => Forbidden,
  404 => NotFound,
  405 => MethodNotAllowed,
  406 => NotAcceptable,
  410 => Gone,
  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



69
70
71
72
# File 'app/services/api/taxjar/error.rb', line 69

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



15
16
17
# File 'app/services/api/taxjar/error.rb', line 15

def code
  @code
end

Class Method Details

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

Build the appropriate error for a non-2xx response. The message is
taken from TaxJar's JSON detail (falling back to error).

Parameters:

  • status (Integer)

    HTTP status code

  • body (Hash, Object)

    parsed response body

Returns:



60
61
62
63
64
# File 'app/services/api/taxjar/error.rb', line 60

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