Class: RESTinPeace::Faraday::RaiseErrorsMiddleware

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/rest_in_peace/faraday/raise_errors_middleware.rb

Constant Summary collapse

CLIENT_ERROR_STATUSES =
400...600

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rest_in_peace/faraday/raise_errors_middleware.rb', line 8

def on_complete(env)
  case env[:status]
  when 404
    raise ::Faraday::ResourceNotFound, response_values(env)
  when 407
    # mimic the behavior that we get with proxy requests with HTTPS
    raise ::Faraday::ConnectionFailed, %{407 "Proxy Authentication Required "}
  when 422
    # do not raise an error as 422 from a rails app means validation errors
    # and response body contains the validation errors
  when CLIENT_ERROR_STATUSES
    raise ::Faraday::ClientError, response_values(env)
  end
end

#response_values(env) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/rest_in_peace/faraday/raise_errors_middleware.rb', line 23

def response_values(env)
  {
    status: env.status,
    headers: env.response_headers,
    body: env.body,
  }
end