Class: Moneybird::Middleware::ErrorHandling

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/moneybird/middleware/error_handling.rb

Constant Summary collapse

ERROR_MAP =
{
  400 => Moneybird::HttpError::BadRequest,
  401 => Moneybird::HttpError::AuthorizationRequired,
  402 => Moneybird::HttpError::PaymentRequired,
  403 => Moneybird::HttpError::Forbidden,
  404 => Moneybird::HttpError::NotFound,
  405 => Moneybird::HttpError::MethodNotAllowed,
  406 => Moneybird::HttpError::NotAccepted,
  422 => Moneybird::HttpError::UnprocessableEntity,
  429 => Moneybird::HttpError::TooManyRequests,
  500 => Moneybird::HttpError::InternalServerError
}.freeze

Instance Method Summary collapse

Instance Method Details

#on_complete(response) ⇒ Object

Raises:



19
20
21
22
# File 'lib/moneybird/middleware/error_handling.rb', line 19

def on_complete(response)
  key = response[:status].to_i
  raise ERROR_MAP[key], response_values(response) if ERROR_MAP.key? key
end

#response_values(response) ⇒ Object



24
25
26
# File 'lib/moneybird/middleware/error_handling.rb', line 24

def response_values(response)
  { status: response.status, headers: response.response_headers, body: response.body }
end