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.34.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.33.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.32.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.31.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.30.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.29.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.28.1 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.28.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.27.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.26.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.25.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.24.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.23.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.22.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.21.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.20.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.19.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.18.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.17.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.16.0 lib/erp_integration/middleware/error_handling.rb