Sha256: ec7ab63166d0e6fd233a03d4b5888e1d51f1c44520fcec45caf7e90f9b822cff

Contents?: true

Size: 1.01 KB

Versions: 18

Compression:

Stored size: 1.01 KB

Contents

require 'faraday'

module RESTinPeace
  module Faraday
    class RaiseErrorsMiddleware < ::Faraday::Response::Middleware
      CLIENT_ERROR_STATUSES = 400...600

      def on_complete(env)
        case env[:status]
        when 404
          raise ::Faraday::Error::ResourceNotFound, response_values(env)
        when 407
          # mimic the behavior that we get with proxy requests with HTTPS
          raise ::Faraday::Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
        when 422
          # do not raise an error as 422 from a rails app means validation errors
          # and response body contains the validation errors
        when CLIENT_ERROR_STATUSES
          raise ::Faraday::Error::ClientError, response_values(env)
        end
      end

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

    ::Faraday::Response.register_middleware rip_raise_errors: RaiseErrorsMiddleware
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
rest-in-peace-6.0.4 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-6.0.3 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-6.0.2 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-6.0.0 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-5.1.0 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-5.0.2 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-5.0.1 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-5.0.0 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-4.2.1 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-4.2.0 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-4.1.1 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-4.1.0 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-4.0.0 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-3.0.0 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-2.0.4 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-2.0.3 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-2.0.2 lib/rest_in_peace/faraday/raise_errors_middleware.rb
rest-in-peace-2.0.1 lib/rest_in_peace/faraday/raise_errors_middleware.rb