Class: Response::RaiseReadmillError

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

Overview

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

Examples

connection = Faraday.new({}) do |conn|
  conn.use Faraday::Response::RaiseReadmillError
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.



44
45
46
47
48
49
50
# File 'lib/faraday/response/raise_readmill_error.rb', line 44

def error_message(response)
  method = response[:method].to_s.upcase
  url = response[:url].to_s
  status = response[:body][:status]
  error = response[:body][:error]
  "#{method} #{url}: #{status} - #{error}"
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.

Returns nothing.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/faraday/response/raise_readmill_error.rb', line 21

def on_complete(response)
  return if response[:body].nil?

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