Sha256: 465f47b47a6a6d3a2cb8f0a178d3e7461885e671db7f5490ca307cff15a1f70e

Contents?: true

Size: 1.13 KB

Versions: 14

Compression:

Stored size: 1.13 KB

Contents

require 'faraday'

# @private
module Faraday
  # @private
  class Response::RaiseHttp4xx < Response::Middleware
    def on_complete(env)
      case env[:status].to_i
      when 400
        raise FullContact::BadRequest.new(error_message(env), env[:response_headers])
      when 401
        raise FullContact::Unauthorized.new(error_message(env), env[:response_headers])
      when 403
        raise FullContact::Forbidden.new(error_message(env), env[:response_headers])
      when 404
        raise FullContact::NotFound.new(error_message(env), env[:response_headers])
      when 422
        raise FullContact::Invalid.new(error_message(env), env[:response_headers])
    end


	end

	private

	def error_message(env)
      "#{env[:method].to_s.upcase} #{env[:url].to_s}: #{env[:status]}#{error_body(env[:body])}"
    end

    def error_body(body)
      if body.nil?
        nil
      elsif body['error']
        ": #{body['error']}"
      elsif body['errors']
        first = Array(body['errors']).first
        if first.kind_of? Hash
          ": #{first['message'].chomp}"
        else
          ": #{first.chomp}"
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
fullcontact-0.7.0 lib/faraday/response/raise_http_4xx.rb
fullcontact-0.6.1 lib/faraday/response/raise_http_4xx.rb
fullcontact-0.6.0 lib/faraday/response/raise_http_4xx.rb
fullcontact-0.5.0 lib/faraday/response/raise_http_4xx.rb
fullcontact-0.4.4 lib/faraday/response/raise_http_4xx.rb
fullcontact-0.4.1 lib/faraday/response/raise_http_4xx.rb
fullcontact-0.3.3 lib/faraday/response/raise_http_4xx.rb
fullcontact-0.3.2 lib/faraday/response/raise_http_4xx.rb
fullcontacter-0.3.2 lib/faraday/response/raise_http_4xx.rb
fullcontact-0.3.1 lib/faraday/response/raise_http_4xx.rb
fullcontact-0.3.0 lib/faraday/response/raise_http_4xx.rb
fullcontact-0.2.0 lib/faraday/response/raise_http_4xx.rb
fullcontact-0.1.4 lib/faraday/response/raise_http_4xx.rb
fullcontact-0.1.3 lib/faraday/response/raise_http_4xx.rb