mirror of
https://github.com/lostisland/faraday.git
synced 2025-10-04 00:02:03 -04:00
Include request info in exceptions raised by RaiseError Middleware (#1181)
This commit is contained in:
parent
868fe9bb18
commit
8ee406d788
@ -38,6 +38,14 @@ module Faraday
|
||||
# :headers - String key/value hash of HTTP response header
|
||||
# values.
|
||||
# :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
|
||||
# to `super`. See NilStatusError.
|
||||
|
@ -38,7 +38,18 @@ module Faraday
|
||||
end
|
||||
|
||||
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
|
||||
|
@ -103,4 +103,37 @@ RSpec.describe Faraday::Response::RaiseError do
|
||||
expect(ex.response[:status]).to eq(500)
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user