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.57.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.56.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.55.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.54.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.53.1 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.53.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.52.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.50.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.49.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.48.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.43.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.42.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.41.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.40.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.39.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.38.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.37.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.36.1 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.36.0 lib/erp_integration/middleware/error_handling.rb
erp_integration-0.35.0 lib/erp_integration/middleware/error_handling.rb