Sha256: 19704e2fcbb1f874ac29f98531097c054bd08fd15ce2c35217f0e155bbe44183

Contents?: true

Size: 1.25 KB

Versions: 9

Compression:

Stored size: 1.25 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 AngellistApi::BadRequest.new(error_message(env), env[:response_headers])
      when 401
        raise AngellistApi::Unauthorized.new(error_message(env), env[:response_headers])
      when 403
        raise AngellistApi::Forbidden.new(error_message(env), env[:response_headers])
      when 404
        raise AngellistApi::NotFound.new(error_message(env), env[:response_headers])
      when 406
        raise AngellistApi::NotAcceptable.new(error_message(env), env[:response_headers])
      when 420
        raise AngellistApi::EnhanceYourCalm.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

9 entries across 9 versions & 1 rubygems

Version Path
angellist_api-1.0.1 lib/faraday/response/raise_http_4xx.rb
angellist_api-0.1.2 lib/faraday/response/raise_http_4xx.rb
angellist_api-1.0.0 lib/faraday/response/raise_http_4xx.rb
angellist_api-0.1.0 lib/faraday/response/raise_http_4xx.rb
angellist_api-0.0.9 lib/faraday/response/raise_http_4xx.rb
angellist_api-0.0.8 lib/faraday/response/raise_http_4xx.rb
angellist_api-0.0.7 lib/faraday/response/raise_http_4xx.rb
angellist_api-0.0.6 lib/faraday/response/raise_http_4xx.rb
angellist_api-0.0.5 lib/faraday/response/raise_http_4xx.rb