diff --git a/lib/faraday/error.rb b/lib/faraday/error.rb index 8cf98977..2facd945 100644 --- a/lib/faraday/error.rb +++ b/lib/faraday/error.rb @@ -79,12 +79,25 @@ module Faraday # Pulls out potential parent exception and response hash. def exc_msg_and_response(exc, response = nil) - return [exc, exc.message, response] if exc.respond_to?(:backtrace) + if exc.is_a?(Exception) + [exc, exc.message, response] + elsif exc.is_a?(Hash) + http_status = exc.fetch(:status) - return [nil, "the server responded with status #{exc[:status]}", exc] \ - if exc.respond_to?(:each_key) + request = exc.fetch(:request, nil) - [nil, exc.to_s, response] + if request.nil? + exception_message = "the server responded with status #{http_status} - method and url are not available " \ + 'due to include_request: false on Faraday::Response::RaiseError middleware' + else + exception_message = "the server responded with status #{http_status} for #{request.fetch(:method).upcase} " \ + "#{request.fetch(:url)}" + end + + [nil, exception_message, exc] + else + [nil, exc.to_s, response] + end end end diff --git a/spec/faraday/error_spec.rb b/spec/faraday/error_spec.rb index 170f3267..05285f76 100644 --- a/spec/faraday/error_spec.rb +++ b/spec/faraday/error_spec.rb @@ -23,7 +23,7 @@ RSpec.describe Faraday::Error do it { expect(subject.wrapped_exception).to be_nil } it { expect(subject.response).to eq(exception) } - it { expect(subject.message).to eq('the server responded with status 400') } + 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') } if RUBY_VERSION >= '3.4' it { expect(subject.inspect).to eq('#') } else diff --git a/spec/faraday/response/raise_error_spec.rb b/spec/faraday/response/raise_error_spec.rb index 6013080d..18c2044a 100644 --- a/spec/faraday/response/raise_error_spec.rb +++ b/spec/faraday/response/raise_error_spec.rb @@ -28,7 +28,7 @@ RSpec.describe Faraday::Response::RaiseError do it 'raises Faraday::BadRequestError for 400 responses' do expect { conn.get('bad-request') }.to raise_error(Faraday::BadRequestError) do |ex| - expect(ex.message).to eq('the server responded with status 400') + expect(ex.message).to eq('the server responded with status 400 for GET http:/bad-request') expect(ex.response[:headers]['X-Reason']).to eq('because') expect(ex.response[:status]).to eq(400) expect(ex.response_status).to eq(400) @@ -39,7 +39,7 @@ RSpec.describe Faraday::Response::RaiseError do it 'raises Faraday::UnauthorizedError for 401 responses' do expect { conn.get('unauthorized') }.to raise_error(Faraday::UnauthorizedError) do |ex| - expect(ex.message).to eq('the server responded with status 401') + expect(ex.message).to eq('the server responded with status 401 for GET http:/unauthorized') expect(ex.response[:headers]['X-Reason']).to eq('because') expect(ex.response[:status]).to eq(401) expect(ex.response_status).to eq(401) @@ -50,7 +50,7 @@ RSpec.describe Faraday::Response::RaiseError do it 'raises Faraday::ForbiddenError for 403 responses' do expect { conn.get('forbidden') }.to raise_error(Faraday::ForbiddenError) do |ex| - expect(ex.message).to eq('the server responded with status 403') + expect(ex.message).to eq('the server responded with status 403 for GET http:/forbidden') expect(ex.response[:headers]['X-Reason']).to eq('because') expect(ex.response[:status]).to eq(403) expect(ex.response_status).to eq(403) @@ -61,7 +61,7 @@ RSpec.describe Faraday::Response::RaiseError do it 'raises Faraday::ResourceNotFound for 404 responses' do expect { conn.get('not-found') }.to raise_error(Faraday::ResourceNotFound) do |ex| - expect(ex.message).to eq('the server responded with status 404') + expect(ex.message).to eq('the server responded with status 404 for GET http:/not-found') expect(ex.response[:headers]['X-Reason']).to eq('because') expect(ex.response[:status]).to eq(404) expect(ex.response_status).to eq(404) @@ -83,7 +83,7 @@ RSpec.describe Faraday::Response::RaiseError do it 'raises Faraday::RequestTimeoutError for 408 responses' do expect { conn.get('request-timeout') }.to raise_error(Faraday::RequestTimeoutError) do |ex| - expect(ex.message).to eq('the server responded with status 408') + expect(ex.message).to eq('the server responded with status 408 for GET http:/request-timeout') expect(ex.response[:headers]['X-Reason']).to eq('because') expect(ex.response[:status]).to eq(408) expect(ex.response_status).to eq(408) @@ -94,7 +94,7 @@ RSpec.describe Faraday::Response::RaiseError do it 'raises Faraday::ConflictError for 409 responses' do expect { conn.get('conflict') }.to raise_error(Faraday::ConflictError) do |ex| - expect(ex.message).to eq('the server responded with status 409') + expect(ex.message).to eq('the server responded with status 409 for GET http:/conflict') expect(ex.response[:headers]['X-Reason']).to eq('because') expect(ex.response[:status]).to eq(409) expect(ex.response_status).to eq(409) @@ -105,7 +105,7 @@ RSpec.describe Faraday::Response::RaiseError do it 'raises Faraday::UnprocessableEntityError for 422 responses' do expect { conn.get('unprocessable-entity') }.to raise_error(Faraday::UnprocessableEntityError) do |ex| - expect(ex.message).to eq('the server responded with status 422') + expect(ex.message).to eq('the server responded with status 422 for GET http:/unprocessable-entity') expect(ex.response[:headers]['X-Reason']).to eq('because') expect(ex.response[:status]).to eq(422) expect(ex.response_status).to eq(422) @@ -116,7 +116,7 @@ RSpec.describe Faraday::Response::RaiseError do it 'raises Faraday::TooManyRequestsError for 429 responses' do expect { conn.get('too-many-requests') }.to raise_error(Faraday::TooManyRequestsError) do |ex| - expect(ex.message).to eq('the server responded with status 429') + expect(ex.message).to eq('the server responded with status 429 for GET http:/too-many-requests') expect(ex.response[:headers]['X-Reason']).to eq('because') expect(ex.response[:status]).to eq(429) expect(ex.response_status).to eq(429) @@ -138,7 +138,7 @@ RSpec.describe Faraday::Response::RaiseError do it 'raises Faraday::ClientError for other 4xx responses' do expect { conn.get('4xx') }.to raise_error(Faraday::ClientError) do |ex| - expect(ex.message).to eq('the server responded with status 499') + expect(ex.message).to eq('the server responded with status 499 for GET http:/4xx') expect(ex.response[:headers]['X-Reason']).to eq('because') expect(ex.response[:status]).to eq(499) expect(ex.response_status).to eq(499) @@ -149,7 +149,7 @@ RSpec.describe Faraday::Response::RaiseError do it 'raises Faraday::ServerError for 500 responses' do expect { conn.get('server-error') }.to raise_error(Faraday::ServerError) do |ex| - expect(ex.message).to eq('the server responded with status 500') + expect(ex.message).to eq('the server responded with status 500 for GET http:/server-error') expect(ex.response[:headers]['X-Error']).to eq('bailout') expect(ex.response[:status]).to eq(500) expect(ex.response_status).to eq(500)