Class: Faraday::Response::RaiseError

Inherits:
Middleware show all
Defined in:
lib/faraday/response/raise_error.rb

Overview

RaiseError is a Faraday middleware that raises exceptions on common HTTP client or server error responses.

Constant Summary collapse

ClientErrorStatuses =

rubocop:disable Naming/ConstantName

(400...500).freeze
ServerErrorStatuses =
(500...600).freeze

Instance Attribute Summary

Attributes inherited from Middleware

#app, #options

Attributes included from DependencyLoader

#load_error

Instance Method Summary collapse

Methods inherited from Middleware

#call, #close, #initialize

Methods included from DependencyLoader

#dependency, #inherited, #loaded?, #new

Methods included from MiddlewareRegistry

#fetch_middleware, #load_middleware, #lookup_middleware, #middleware_mutex, #register_middleware, #unregister_middleware

Constructor Details

This class inherits a constructor from Faraday::Middleware

Instance Method Details

#on_complete(env) ⇒ Object

rubocop:enable Naming/ConstantName


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/faraday/response/raise_error.rb', line 13

def on_complete(env)
  case env[:status]
  when 400
    raise Faraday::BadRequestError, response_values(env)
  when 401
    raise Faraday::UnauthorizedError, response_values(env)
  when 403
    raise Faraday::ForbiddenError, response_values(env)
  when 404
    raise Faraday::ResourceNotFound, response_values(env)
  when 407
    # mimic the behavior that we get with proxy requests with HTTPS
    msg = %(407 "Proxy Authentication Required")
    raise Faraday::ProxyAuthError.new(msg, response_values(env))
  when 409
    raise Faraday::ConflictError, response_values(env)
  when 422
    raise Faraday::UnprocessableEntityError, response_values(env)
  when ClientErrorStatuses
    raise Faraday::ClientError, response_values(env)
  when ServerErrorStatuses
    raise Faraday::ServerError, response_values(env)
  when nil
    raise Faraday::NilStatusError, response_values(env)
  end
end

#response_values(env) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/faraday/response/raise_error.rb', line 40

def response_values(env)
  {
    status: env.status,
    headers: env.response_headers,
    body: env.body,
    request: {
      method: env.method,
      url_path: env.url.path,
      params: env.params,
      headers: env.request_headers,
      body: env.request_body
    }
  }
end