Class: Faraday::Conductivity::SelectiveErrors
- Inherits:
-
Middleware
- Object
- Middleware
- Faraday::Conductivity::SelectiveErrors
- Defined in:
- lib/faraday/conductivity/selective_errors.rb
Overview
Use this to raise errors on certain HTTP statuses. These are basically the same errors as Faraday raises when you use the “raise_error” middleware, but with added fields to better inspect what went wrong.
Examples:
# specify an array
faraday.response :selective_errors, on: [422,500]
# or a range:
faraday.response :selective_errors, on: 500...600
# specify errors:
faraday.response :selective_errors, except: [404,422]
Rescueing the errors:
begin
do_request
rescue Faraday::Conductivity::Error => error
puts error.request[:url]
puts error.request[:method]
puts error.request[:body]
puts error.request[:headers]
puts error.response[:status]
puts error.response[:body]
puts error.response[:headers]
end
Constant Summary collapse
- ClientErrorStatuses =
(400...500).freeze
- ServerErrorStatuses =
(500...600).freeze
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ SelectiveErrors
constructor
A new instance of SelectiveErrors.
- #response_values(env) ⇒ Object
- #should_raise_error?(status) ⇒ Boolean
Constructor Details
#initialize(app, options = {}) ⇒ SelectiveErrors
Returns a new instance of SelectiveErrors.
60 61 62 63 64 |
# File 'lib/faraday/conductivity/selective_errors.rb', line 60 def initialize(app, = {}) @app = app @on = .fetch(:on) { ClientErrorStatuses.to_a + ServerErrorStatuses.to_a } @except = .fetch(:except) { [] } end |
Instance Method Details
#call(env) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/faraday/conductivity/selective_errors.rb', line 66 def call(env) # capture request because it will be modified during the request request = { :method => env[:method], :url => env[:url], :body => env[:body], :headers => env[:request_headers], } start_time = Time.now @app.call(env).on_complete do status = env[:status] if should_raise_error?(status) error = case status when 400 Faraday::BadRequestError.new(response_values(env)) when 401 Faraday::UnauthorizedError.new(response_values(env)) when 403 Faraday::ForbiddenError.new(response_values(env)) when 404 Faraday::ResourceNotFound.new(response_values(env)) when 407 # mimic the behavior that we get with proxy requests with HTTPS msg = %(407 "Proxy Authentication Required") Faraday::ProxyAuthError.new(msg, response_values(env)) when 409 Faraday::ConflictError.new(response_values(env)) when 422 Faraday::UnprocessableEntityError.new(response_values(env)) when ClientErrorStatuses Faraday::ClientError.new(response_values(env)) when ServerErrorStatuses Faraday::ServerError.new(response_values(env)) when nil Faraday::NilStatusError.new(response_values(env)) end error.extend Error error.response = response_values(env) error.request = request error.response_time = Time.now - start_time raise error end end end |
#response_values(env) ⇒ Object
117 118 119 |
# File 'lib/faraday/conductivity/selective_errors.rb', line 117 def response_values(env) { status: env.status, headers: env.response_headers, body: env.body } end |
#should_raise_error?(status) ⇒ Boolean
121 122 123 |
# File 'lib/faraday/conductivity/selective_errors.rb', line 121 def should_raise_error?(status) @on.include?(status) && !@except.include?(status) end |