Sha256: ed14b01246c8a5064dc2250e61310bf9c7385586b81112174de31e7a0c65a9ea

Contents?: true

Size: 1.24 KB

Versions: 21

Compression:

Stored size: 1.24 KB

Contents

require 'faraday'

module Faraday

  class EdsExceptionMiddleware < Faraday::Middleware

  def initialize(app)
      super app
    end

    def call(env)
      begin
        @app.call(env).on_complete do |response|
          case response.status
            when 200
            when 400
              raise EBSCO::EDS::BadRequest.new(error_message(response))
            # when 401
            #   raise EBSCO::EDS::Unauthorized.new
            # when 403
            #   raise EBSCO::EDS::Forbidden.new
            # when 404
            #   raise EBSCO::EDS::NotFound.new
            # when 429
            #   raise EBSCO::EDS::TooManyRequests.new
            when 500
              raise EBSCO::EDS::InternalServerError.new
            when 503
              raise EBSCO::EDS::ServiceUnavailable.new
            else
              raise EBSCO::EDS::BadRequest.new(error_message(response))
          end
        end
        rescue Faraday::ConnectionFailed
          raise EBSCO::EDS::ConnectionFailed.new
      end
    end

    private

    def error_message(response)
      #puts response.inspect
      {
          method: response.method,
          url: response.url,
          status: response.status,
          error_body: response.body
      }
    end

  end

end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
ebsco-eds-1.0.7 lib/faraday/eds_exception_middleware.rb
ebsco-eds-1.0.6 lib/faraday/eds_exception_middleware.rb
ebsco-eds-1.0.5 lib/faraday/eds_exception_middleware.rb
ebsco-eds-1.0.4 lib/faraday/eds_exception_middleware.rb
ebsco-eds-1.0.3 lib/faraday/eds_exception_middleware.rb
ebsco-eds-1.0.2 lib/faraday/eds_exception_middleware.rb
ebsco-eds-1.0.1 lib/faraday/eds_exception_middleware.rb
ebsco-eds-1.0.0 lib/faraday/eds_exception_middleware.rb
ebsco-eds-0.3.19.pre lib/faraday/eds_exception_middleware.rb
ebsco-eds-0.3.18.pre lib/faraday/eds_exception_middleware.rb
ebsco-eds-0.3.17.pre lib/faraday/eds_exception_middleware.rb
ebsco-eds-0.3.16.pre lib/faraday/eds_exception_middleware.rb
ebsco-eds-0.3.15.pre lib/faraday/eds_exception_middleware.rb
ebsco-eds-0.3.14.pre lib/faraday/eds_exception_middleware.rb
ebsco-eds-0.3.13.pre lib/faraday/eds_exception_middleware.rb
ebsco-eds-0.3.12.pre lib/faraday/eds_exception_middleware.rb
ebsco-eds-0.3.11.pre lib/faraday/eds_exception_middleware.rb
ebsco-eds-0.3.10.pre lib/faraday/eds_exception_middleware.rb
ebsco-eds-0.3.9.pre lib/faraday/eds_exception_middleware.rb
ebsco-eds-0.3.8.pre lib/faraday/eds_exception_middleware.rb