Class: PaypalClient::Errors::Middleware

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/paypal_client/errors.rb

Constant Summary collapse

ERROR_MAP =
{
  400 => InvalidRequest,
  401 => AuthenticationFailure,
  403 => NotAuthorized,
  404 => ResourceNotFound,
  405 => MethodNotSupported,
  406 => MediaTypeNotAcceptable,
  415 => UnsupportedMediaType,
  422 => UnprocessableEntity,
  429 => RateLimitReached,
  500 => InternalServerError,
  503 => ServiceUnavailable
}.freeze
ERRORS_WITHOUT_BODY =
[404].freeze

Instance Method Summary collapse

Instance Method Details

#on_complete(response) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/paypal_client/errors.rb', line 64

def on_complete(response)
  status = response[:status].to_i
  if status >= 400
    error = ERROR_MAP.key?(status) ? ERROR_MAP[status] : Error

    body = response.body
    raise error.new(get_error_message(response), code: get_error_code(response), http_status: status, http_body: body)
  end
end