class FaradayMiddleware::RaiseHttpException

Public Class Methods

new(app) click to toggle source
Calls superclass method
# File lib/faraday/raise_http_exception.rb, line 37
def initialize(app)
  super app
  @parser = nil
end

Public Instance Methods

call(env) click to toggle source
# File lib/faraday/raise_http_exception.rb, line 6
def call(env)
  @app.call(env).on_complete do |response|
    case response[:status].to_i
    when 400
      raise Taxjar::BadRequest, error_message_400(response)
    when 401
      raise Taxjar::NotAuthorized, error_message_400(response)
    when 403
      raise Taxjar::Forbidden, error_message_400(response)
    when 404
      raise Taxjar::NotFound, error_message_400(response)
    when 405
      raise Taxjar::MethodNotAllowed, error_message_400(response)
    when 406
      raise Taxjar::NotAcceptable, error_message_400(response)
    when 410
      raise Taxjar::Gone, error_message_400(response)
    when 422
      raise Taxjar::UnprocessableEntity, error_message_400(response)
    when 429
      raise Taxjar::TooManyRequests, error_message_400(response)
    when 500
      raise Taxjar::InternalServerError, error_message_500(response, "Something is technically wrong.")
    when 503
      raise Taxjar::ServiceUnavailable, error_message_500(response, "Service is temporarily down for maintenance.")
    when 504
      raise Taxjar::GatewayTimeout, error_message_500(response, "504 Gateway Time-out")
    end
  end
end

Private Instance Methods

error_body(body) click to toggle source
# File lib/faraday/raise_http_exception.rb, line 48
def error_body(body)

  body = ::JSON.parse(body) if not body.nil? and not body.empty? and body.kind_of?(String)

  if body.nil?
    nil
  elsif body['meta'] and body['meta']['error_message'] and not body['meta']['error_message'].empty?
    ": #{body['meta']['error_message']}"
  elsif body['error_message'] and not body['error_message'].empty?
    ": #{body['error_type']}: #{body['error_message']}"
  end
end
error_message_400(response) click to toggle source
# File lib/faraday/raise_http_exception.rb, line 44
def error_message_400(response)
  "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{error_body(response[:body])}"
end
error_message_500(response, body=nil) click to toggle source
# File lib/faraday/raise_http_exception.rb, line 61
def error_message_500(response, body=nil)
  "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{[response[:status].to_s + ':', body].compact.join(' ')}"
end