Sha256: cd9ccc00efb0b27b076b01b642e1fb3ba470c3f173f986e60fda905b40c5e9bc

Contents?: true

Size: 1003 Bytes

Versions: 1

Compression:

Stored size: 1003 Bytes

Contents

require 'faraday'

# @api private
module Faraday
  class Response::RaiseError < Response::Middleware
    def on_complete(response)
      case response[:status].to_i
      when 400
        raise SBA::BadRequest, error_message(response)
      when 401
        raise SBA::Unauthorized, error_message(response)
      when 403
        raise SBA::Forbidden, error_message(response)
      when 404
        raise SBA::NotFound, error_message(response)
      when 406
        raise SBA::NotAcceptable, error_message(response)
      when 500
        raise SBA::InternalServerError, error_message(response)
      when 502
        raise SBA::BadGateway, error_message(response)
      when 503
        raise SBA::ServiceUnavailable, error_message(response)
      end
    end

    def error_message(response)
      "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:response_headers]['status']}#{(': ' + response[:body]['error']) if response[:body] && response[:body]['error']}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sba-0.0.1 lib/faraday/response/raise_error.rb