Sha256: 932f86c366d29525dd2bfc4608a400d61378e08595597434cfe028fe797ec0ce

Contents?: true

Size: 1.2 KB

Versions: 62

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module ErpIntegration
  module Middleware
    # The `ErrorHandling` middleware allows to raise our own exceptions that
    # are easier to catch for the host application (e.g. `mejuri-web`).
    class ErrorHandling < Faraday::Response::Middleware
      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

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

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

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
erp_integration-0.15.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.14.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.13.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.12.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.11.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.10.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.9.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.8.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.7.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.6.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.5.5 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.5.4 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.5.3 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.5.2 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.5.1 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.5.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.4.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.3.3 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.3.2 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.3.1 lib/erp_integration/middleware/error_handling.rb