Sha256: 0977e1116462691066fa3fa8d5b597931865889ce3369de86a68d22645073165

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 KB

Contents

module Pupa
  class Processor
    module Middleware
      # A Faraday response middleware for raising an error if unsuccessful.
      #
      # @see Faraday::Response::RaiseError
      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

5 entries across 5 versions & 1 rubygems

Version Path
pupa-0.0.13 lib/pupa/processor/middleware/raise_error.rb
pupa-0.0.12 lib/pupa/processor/middleware/raise_error.rb
pupa-0.0.11 lib/pupa/processor/middleware/raise_error.rb
pupa-0.0.10 lib/pupa/processor/middleware/raise_error.rb
pupa-0.0.9 lib/pupa/processor/middleware/raise_error.rb