Class: Faraday::Response::RaiseError
- Inherits:
-
Middleware
- Object
- Middleware
- Faraday::Response::RaiseError
- 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
Instance Method Summary collapse
-
#on_complete(env) ⇒ Object
rubocop:enable Naming/ConstantName.
- #query_params(env) ⇒ Object
-
#response_values(env) ⇒ Object
Returns a hash of response data with the following keys: - status - headers - body - request.
Methods inherited from Middleware
Methods included from MiddlewareRegistry
#lookup_middleware, #register_middleware, #registered_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 39 40 |
# 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 408 raise Faraday::RequestTimeoutError, 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 |
#query_params(env) ⇒ Object
73 74 75 76 |
# File 'lib/faraday/response/raise_error.rb', line 73 def query_params(env) env.request.params_encoder ||= Faraday::Utils.default_params_encoder env.params_encoder.decode(env.url.query) end |
#response_values(env) ⇒ Object
Returns a hash of response data with the following keys:
- status
- headers
- body
- request
The request
key is omitted when the middleware is explicitly
configured with the option include_request: false
.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/faraday/response/raise_error.rb', line 50 def response_values(env) response = { status: env.status, headers: env.response_headers, body: env.body } # Include the request data by default. If the middleware was explicitly # configured to _not_ include request data, then omit it. return response unless .fetch(:include_request, true) response.merge( request: { method: env.method, url: env.url, url_path: env.url.path, params: query_params(env), headers: env.request_headers, body: env.request_body } ) end |