Class: Response::RaiseESPNError

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/response/raise_espn_error.rb

Overview

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

Examples

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

Instance Method Summary collapse

Instance Method Details

#error_message(response) ⇒ Object

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

Returns a String.



45
46
47
# 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) ⇒ Object

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.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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