Include request info in exceptions raised by RaiseError Middleware (#1181)

This commit is contained in:
Sandro Damilano 2020-09-17 11:45:25 -03:00 committed by GitHub
parent 868fe9bb18
commit 8ee406d788
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 1 deletions

View File

@ -38,6 +38,14 @@ module Faraday
# :headers - String key/value hash of HTTP response header # :headers - String key/value hash of HTTP response header
# values. # values.
# :body - Optional string HTTP response body. # :body - Optional string HTTP response body.
# :request - Hash
# :method - Symbol with the request HTTP method.
# :url_path - String with the url path requested.
# :params - String key/value hash of query params
# present in the request.
# :headers - String key/value hash of HTTP request
# header values.
# :body - String HTTP request body.
# #
# If a subclass has to call this, then it should pass a string message # If a subclass has to call this, then it should pass a string message
# to `super`. See NilStatusError. # to `super`. See NilStatusError.

View File

@ -38,7 +38,18 @@ module Faraday
end end
def response_values(env) def response_values(env)
{ status: env.status, headers: env.response_headers, body: env.body } {
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 end
end end
end end

View File

@ -103,4 +103,37 @@ RSpec.describe Faraday::Response::RaiseError do
expect(ex.response[:status]).to eq(500) expect(ex.response[:status]).to eq(500)
end end
end end
describe 'request info' do
let(:conn) do
Faraday.new do |b|
b.response :raise_error
b.adapter :test do |stub|
stub.post('request?full=true', request_body, request_headers) do
[400, { 'X-Reason' => 'because' }, 'keep looking']
end
end
end
end
let(:request_body) { JSON.generate({ 'item' => 'sth' }) }
let(:request_headers) { { 'Authorization' => 'Basic 123' } }
subject(:perform_request) do
conn.post 'request' do |req|
req.headers['Authorization'] = 'Basic 123'
req.params[:full] = true
req.body = request_body
end
end
it 'returns the request info in the exception' do
expect { perform_request }.to raise_error(Faraday::BadRequestError) do |ex|
expect(ex.response[:request][:method]).to eq(:post)
expect(ex.response[:request][:url_path]).to eq('/request')
expect(ex.response[:request][:params]).to eq({ 'full' => 'true' })
expect(ex.response[:request][:headers]).to match(a_hash_including(request_headers))
expect(ex.response[:request][:body]).to eq(request_body)
end
end
end
end end