Class: ErpIntegration::Middleware::ErrorHandling

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

Overview

The ‘ErrorHandling` middleware allows to raise our own exceptions that are easier to catch for the host application (e.g. `mejuri-web`).

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#on_complete(response) ⇒ Object

Raises:



21
22
23
24
# File 'lib/erp_integration/middleware/error_handling.rb', line 21

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

#response_values(response) ⇒ Object



26
27
28
# File 'lib/erp_integration/middleware/error_handling.rb', line 26

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