Module: Afterpay::ErrorHandler

Defined in:
lib/afterpay/error_handler.rb

Constant Summary collapse

ERRORS =
{
  400 => BadRequestError,
  401 => UnathorizedError,
  402 => PaymentRequiredError,
  404 => NotFoundError,
  405 => MethodNotAllowedError,
  406 => NotAcceptableError,
  408 => RequestTimeoutError,
  409 => ConflictError,
  410 => GoneError,
  412 => PreconditionFailedError,
  422 => UnprocessableEntityError,
  429 => TooManyRequestsError,
  500 => InternalServerError,
  503 => ServiceUnavailableError,
  504 => GatewayTimeoutError
}.freeze

Class Method Summary collapse

Class Method Details

.inspect(response) ⇒ Afterpay::HTTPService::Response

Returns a response if request was successful, raises an error otherwise

Parameters:

Returns:

Raises:



11
12
13
14
15
# File 'lib/afterpay/error_handler.rb', line 11

def inspect(response)
  return response if response.ok?

  raise ERRORS[response.status].new(response_error_code(response)), response_message(response)
end

.response_error_code(response) ⇒ Object



21
22
23
# File 'lib/afterpay/error_handler.rb', line 21

def response_error_code(response)
  response.body.is_a?(String) ? JSON.parse(response.body)['errorCode'] : response.body.errorCode
end

.response_message(response) ⇒ Object



17
18
19
# File 'lib/afterpay/error_handler.rb', line 17

def response_message(response)
  response.body.is_a?(String) ? JSON.parse(response.body)['message'] : response.message
end