Skip to content

Commit ad8fe1e

Browse files
authored
Include HTTP method and URL in Faraday::Error messages for improved exception log transparency (#1628)
1 parent 1ddd281 commit ad8fe1e

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

lib/faraday/error.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,25 @@ def exc_msg_and_response!(exc, response = nil)
7979

8080
# Pulls out potential parent exception and response hash.
8181
def exc_msg_and_response(exc, response = nil)
82-
return [exc, exc.message, response] if exc.respond_to?(:backtrace)
83-
84-
return [nil, "the server responded with status #{exc[:status]}", exc] \
85-
if exc.respond_to?(:each_key)
86-
87-
[nil, exc.to_s, response]
82+
if exc.is_a?(Exception)
83+
[exc, exc.message, response]
84+
elsif exc.is_a?(Hash)
85+
http_status = exc.fetch(:status)
86+
87+
request = exc.fetch(:request, nil)
88+
89+
if request.nil?
90+
exception_message = "the server responded with status #{http_status} - method and url are not available " \
91+
'due to include_request: false on Faraday::Response::RaiseError middleware'
92+
else
93+
exception_message = "the server responded with status #{http_status} for #{request.fetch(:method).upcase} " \
94+
"#{request.fetch(:url)}"
95+
end
96+
97+
[nil, exception_message, exc]
98+
else
99+
[nil, exc.to_s, response]
100+
end
88101
end
89102
end
90103

spec/faraday/error_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
it { expect(subject.wrapped_exception).to be_nil }
2525
it { expect(subject.response).to eq(exception) }
26-
it { expect(subject.message).to eq('the server responded with status 400') }
26+
it { expect(subject.message).to eq('the server responded with status 400 - method and url are not available due to include_request: false on Faraday::Response::RaiseError middleware') }
2727
if RUBY_VERSION >= '3.4'
2828
it { expect(subject.inspect).to eq('#<Faraday::Error response={status: 400}>') }
2929
else

spec/faraday/response/raise_error_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
it 'raises Faraday::BadRequestError for 400 responses' do
3030
expect { conn.get('bad-request') }.to raise_error(Faraday::BadRequestError) do |ex|
31-
expect(ex.message).to eq('the server responded with status 400')
31+
expect(ex.message).to eq('the server responded with status 400 for GET http:/bad-request')
3232
expect(ex.response[:headers]['X-Reason']).to eq('because')
3333
expect(ex.response[:status]).to eq(400)
3434
expect(ex.response_status).to eq(400)
@@ -39,7 +39,7 @@
3939

4040
it 'raises Faraday::UnauthorizedError for 401 responses' do
4141
expect { conn.get('unauthorized') }.to raise_error(Faraday::UnauthorizedError) do |ex|
42-
expect(ex.message).to eq('the server responded with status 401')
42+
expect(ex.message).to eq('the server responded with status 401 for GET http:/unauthorized')
4343
expect(ex.response[:headers]['X-Reason']).to eq('because')
4444
expect(ex.response[:status]).to eq(401)
4545
expect(ex.response_status).to eq(401)
@@ -50,7 +50,7 @@
5050

5151
it 'raises Faraday::ForbiddenError for 403 responses' do
5252
expect { conn.get('forbidden') }.to raise_error(Faraday::ForbiddenError) do |ex|
53-
expect(ex.message).to eq('the server responded with status 403')
53+
expect(ex.message).to eq('the server responded with status 403 for GET http:/forbidden')
5454
expect(ex.response[:headers]['X-Reason']).to eq('because')
5555
expect(ex.response[:status]).to eq(403)
5656
expect(ex.response_status).to eq(403)
@@ -61,7 +61,7 @@
6161

6262
it 'raises Faraday::ResourceNotFound for 404 responses' do
6363
expect { conn.get('not-found') }.to raise_error(Faraday::ResourceNotFound) do |ex|
64-
expect(ex.message).to eq('the server responded with status 404')
64+
expect(ex.message).to eq('the server responded with status 404 for GET http:/not-found')
6565
expect(ex.response[:headers]['X-Reason']).to eq('because')
6666
expect(ex.response[:status]).to eq(404)
6767
expect(ex.response_status).to eq(404)
@@ -83,7 +83,7 @@
8383

8484
it 'raises Faraday::RequestTimeoutError for 408 responses' do
8585
expect { conn.get('request-timeout') }.to raise_error(Faraday::RequestTimeoutError) do |ex|
86-
expect(ex.message).to eq('the server responded with status 408')
86+
expect(ex.message).to eq('the server responded with status 408 for GET http:/request-timeout')
8787
expect(ex.response[:headers]['X-Reason']).to eq('because')
8888
expect(ex.response[:status]).to eq(408)
8989
expect(ex.response_status).to eq(408)
@@ -94,7 +94,7 @@
9494

9595
it 'raises Faraday::ConflictError for 409 responses' do
9696
expect { conn.get('conflict') }.to raise_error(Faraday::ConflictError) do |ex|
97-
expect(ex.message).to eq('the server responded with status 409')
97+
expect(ex.message).to eq('the server responded with status 409 for GET http:/conflict')
9898
expect(ex.response[:headers]['X-Reason']).to eq('because')
9999
expect(ex.response[:status]).to eq(409)
100100
expect(ex.response_status).to eq(409)
@@ -105,7 +105,7 @@
105105

106106
it 'raises Faraday::UnprocessableEntityError for 422 responses' do
107107
expect { conn.get('unprocessable-entity') }.to raise_error(Faraday::UnprocessableEntityError) do |ex|
108-
expect(ex.message).to eq('the server responded with status 422')
108+
expect(ex.message).to eq('the server responded with status 422 for GET http:/unprocessable-entity')
109109
expect(ex.response[:headers]['X-Reason']).to eq('because')
110110
expect(ex.response[:status]).to eq(422)
111111
expect(ex.response_status).to eq(422)
@@ -116,7 +116,7 @@
116116

117117
it 'raises Faraday::TooManyRequestsError for 429 responses' do
118118
expect { conn.get('too-many-requests') }.to raise_error(Faraday::TooManyRequestsError) do |ex|
119-
expect(ex.message).to eq('the server responded with status 429')
119+
expect(ex.message).to eq('the server responded with status 429 for GET http:/too-many-requests')
120120
expect(ex.response[:headers]['X-Reason']).to eq('because')
121121
expect(ex.response[:status]).to eq(429)
122122
expect(ex.response_status).to eq(429)
@@ -138,7 +138,7 @@
138138

139139
it 'raises Faraday::ClientError for other 4xx responses' do
140140
expect { conn.get('4xx') }.to raise_error(Faraday::ClientError) do |ex|
141-
expect(ex.message).to eq('the server responded with status 499')
141+
expect(ex.message).to eq('the server responded with status 499 for GET http:/4xx')
142142
expect(ex.response[:headers]['X-Reason']).to eq('because')
143143
expect(ex.response[:status]).to eq(499)
144144
expect(ex.response_status).to eq(499)
@@ -149,7 +149,7 @@
149149

150150
it 'raises Faraday::ServerError for 500 responses' do
151151
expect { conn.get('server-error') }.to raise_error(Faraday::ServerError) do |ex|
152-
expect(ex.message).to eq('the server responded with status 500')
152+
expect(ex.message).to eq('the server responded with status 500 for GET http:/server-error')
153153
expect(ex.response[:headers]['X-Error']).to eq('bailout')
154154
expect(ex.response[:status]).to eq(500)
155155
expect(ex.response_status).to eq(500)

0 commit comments

Comments
 (0)