Sha256: 6ace66f28343209f5b05122553623de727a381e493051993af8a07e092e2884d

Contents?: true

Size: 1.08 KB

Versions: 8

Compression:

Stored size: 1.08 KB

Contents

module Pupa
  class Processor
    module Middleware
      # A Faraday response middleware for raising an error if unsuccessful.
      #
      # @see Faraday::Response::RaiseError
      # @note Faraday has no tests for this middleware.
      class RaiseError < Faraday::Response::Middleware
        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 400...600
            raise Faraday::Error::ClientError, response_values(env)
          end
        end

        def response_values(env) # XXX add more keys
          {
            method: env[:method],
            url: env[:url].to_s,
            request_headers: env[:request_headers],
            status: env[:status],
            response_headers: env[:response_headers],
            body: env[:body].to_s,
          }
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
pupa-0.1.7 lib/pupa/processor/middleware/raise_error.rb
pupa-0.1.6 lib/pupa/processor/middleware/raise_error.rb
pupa-0.1.5 lib/pupa/processor/middleware/raise_error.rb
pupa-0.1.4 lib/pupa/processor/middleware/raise_error.rb
pupa-0.1.3 lib/pupa/processor/middleware/raise_error.rb
pupa-0.1.2 lib/pupa/processor/middleware/raise_error.rb
pupa-0.1.1 lib/pupa/processor/middleware/raise_error.rb
pupa-0.1.0 lib/pupa/processor/middleware/raise_error.rb