Sha256: 42f68c3de458a4f8c1a279f805ae56af06d93358a75f2a028c492c665d5e210b

Contents?: true

Size: 1.54 KB

Versions: 19

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module Faraday
  class Response
    # RaiseError is a Faraday middleware that raises exceptions on common HTTP
    # client or server error responses.
    class RaiseError < Middleware
      # rubocop:disable Naming/ConstantName
      ClientErrorStatuses = (400...500).freeze
      ServerErrorStatuses = (500...600).freeze
      # rubocop:enable Naming/ConstantName

      def on_complete(env)
        case env[:status]
        when 400
          raise Faraday::BadRequestError, response_values(env)
        when 401
          raise Faraday::UnauthorizedError, response_values(env)
        when 403
          raise Faraday::ForbiddenError, response_values(env)
        when 404
          raise Faraday::ResourceNotFound, response_values(env)
        when 407
          # mimic the behavior that we get with proxy requests with HTTPS
          msg = %(407 "Proxy Authentication Required")
          raise Faraday::ProxyAuthError.new(msg, response_values(env))
        when 409
          raise Faraday::ConflictError, response_values(env)
        when 422
          raise Faraday::UnprocessableEntityError, response_values(env)
        when ClientErrorStatuses
          raise Faraday::ClientError, response_values(env)
        when ServerErrorStatuses
          raise Faraday::ServerError, response_values(env)
        when nil
          raise Faraday::NilStatusError, response_values(env)
        end
      end

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

Version data entries

19 entries across 18 versions & 8 rubygems

Version Path
alloy_sdk-0.1.0 vendor/bundle/ruby/3.0.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
passbase-1.0.2 vendor/bundle/ruby/2.7.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
passbase-1.0.1 vendor/bundle/ruby/2.7.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
passbase-1.0.0 vendor/bundle/ruby/2.7.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
logstash-filter-csharp-0.2.1 vendor/bundle/jruby/2.5.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
logstash-filter-csharp-0.2.0 vendor/bundle/jruby/2.5.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
tdiary-5.1.3 vendor/bundle/ruby/2.7.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
tdiary-5.1.3 vendor/bundle/ruby/2.6.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
logstash-output-scalyr-0.1.5 vendor/bundle/jruby/2.5.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
logstash-output-scalyr-0.1.4 vendor/bundle/jruby/2.5.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
logstash-output-scalyr-0.1.3 vendor/bundle/jruby/2.5.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
tdiary-5.1.2 vendor/bundle/ruby/2.7.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
logstash-output-scalyr-0.1.2 vendor/bundle/jruby/2.5.0/gems/faraday-1.0.1/lib/faraday/response/raise_error.rb
faraday-1.0.1 lib/faraday/response/raise_error.rb
tdiary-5.1.1 vendor/bundle/ruby/2.7.0/gems/faraday-1.0.0/lib/faraday/response/raise_error.rb
faraday-1.0.0 lib/faraday/response/raise_error.rb