Skip to content

Commit 7d15a00

Browse files
willianzocolaujrochkind
authored andcommitted
Improve request info in exceptions raised by RaiseError Middleware (lostisland#1335)
1 parent c2ed23d commit 7d15a00

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

lib/faraday/error.rb

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def response_body
5252
# :body - Optional string HTTP response body.
5353
# :request - Hash
5454
# :method - Symbol with the request HTTP method.
55+
# :url - URI object with the url requested.
5556
# :url_path - String with the url path requested.
5657
# :params - String key/value hash of query params
5758
# present in the request.

lib/faraday/response/raise_error.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,19 @@ def response_values(env)
4444
body: env.body,
4545
request: {
4646
method: env.method,
47+
url: env.url,
4748
url_path: env.url.path,
48-
params: env.params,
49+
params: query_params(env),
4950
headers: env.request_headers,
5051
body: env.request_body
5152
}
5253
}
5354
end
55+
56+
def query_params(env)
57+
env.request.params_encoder ||= Faraday::Utils.default_params_encoder
58+
env.params_encoder.decode(env.url.query)
59+
end
5460
end
5561
end
5662
end

spec/faraday/response/raise_error_spec.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -139,27 +139,30 @@
139139
Faraday.new do |b|
140140
b.response :raise_error
141141
b.adapter :test do |stub|
142-
stub.post('request?full=true', request_body, request_headers) do
142+
stub.post(url, request_body, request_headers) do
143143
[400, { 'X-Reason' => 'because' }, 'keep looking']
144144
end
145145
end
146146
end
147147
end
148148
let(:request_body) { JSON.generate({ 'item' => 'sth' }) }
149149
let(:request_headers) { { 'Authorization' => 'Basic 123' } }
150+
let(:url_path) { 'request' }
151+
let(:query_params) { 'full=true' }
152+
let(:url) { "#{url_path}?#{query_params}" }
150153

151154
subject(:perform_request) do
152-
conn.post 'request' do |req|
155+
conn.post url do |req|
153156
req.headers['Authorization'] = 'Basic 123'
154-
req.params[:full] = true
155157
req.body = request_body
156158
end
157159
end
158160

159161
it 'returns the request info in the exception' do
160162
expect { perform_request }.to raise_error(Faraday::BadRequestError) do |ex|
161163
expect(ex.response[:request][:method]).to eq(:post)
162-
expect(ex.response[:request][:url_path]).to eq('/request')
164+
expect(ex.response[:request][:url]).to eq(URI("http:/#{url}"))
165+
expect(ex.response[:request][:url_path]).to eq("/#{url_path}")
163166
expect(ex.response[:request][:params]).to eq({ 'full' => 'true' })
164167
expect(ex.response[:request][:headers]).to match(a_hash_including(request_headers))
165168
expect(ex.response[:request][:body]).to eq(request_body)

0 commit comments

Comments
 (0)