class Faraday::Response::RaiseESPNError

Internal: Faraday Middleware that deals with errors coming back from the API.

Examples

conn = Faraday.new({}) do |builder|
  builder.use Faraday::Response::RaiseESPNError
end

Public Instance Methods

error_message(response) click to toggle source

Internal: Parse the response and return a human friendly String representing the error that we received.

Returns a String.

# File lib/faraday/response/raise_espn_error.rb, line 45
def error_message(response)
  "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:body][:status]} - #{response[:body][:message]}"
end
on_complete(response) click to toggle source

Internal: Hook into the complete callback for the client. When the result comes back, if it is a status code that we want to raise an error for, we will do that.

Raises ESPN::Unauthorized, ESPN::Forbidden and ESPN::Notfound. Returns nothing.

# File lib/faraday/response/raise_espn_error.rb, line 22
def on_complete(response)
  return if response[:body].nil?

  case response[:body][:code].to_i
  when 400
    raise ESPN::BadRequest, error_message(response)
  when 401
    raise ESPN::Unauthorized, error_message(response)
  when 403
    raise ESPN::Forbidden, error_message(response)
  when 404
    raise ESPN::NotFound, error_message(response)
  when 500
    raise ESPN::InternalServerError, error_message(response)
  when 504
    raise ESPN::GatewayTimeout, error_message(response)
  end
end