Skip to content

Commit

Permalink
Guard against other Faraday exception response formats
Browse files Browse the repository at this point in the history
  • Loading branch information
dalibor committed Dec 13, 2023
1 parent 056e45a commit 45deb8a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/elastic_apm/spies/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ def run_request(method, url, body, headers, &block)
yield req if block
end
rescue Faraday::ClientError, Faraday::ServerError => e # Faraday::Response::RaiseError
status = e.response_status if e.respond_to?(:response_status)
status ||= e.response&.fetch(:status)
status =
if e.respond_to?(:response_status)
e.response_status
elsif e.response && e.response.respond_to?(:status)
e.response.status
elsif e.response && e.response.respond_to?(:fetch)
e.response[:status]
end
http = span&.context&.http
if http && status
http.status_code = status.to_s
Expand Down
75 changes: 73 additions & 2 deletions spec/elastic_apm/spies/faraday_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ module ElasticAPM
end
end

it 'should capture status_code' do
it 'captures status_code' do
WebMock.stub_request(:get, 'http://example.com')
.to_return(status: [404, 'Not Found'])

Expand All @@ -217,7 +217,7 @@ module ElasticAPM
expect(http.status_code).to match('404')
end

it 'should handle a nil response' do
it 'handles a nil response' do
WebMock.stub_request(:get, 'http://example.com')
.to_raise(Faraday::ClientError)

Expand All @@ -235,6 +235,77 @@ module ElasticAPM
expect(http.status_code).to be nil
end

it 'handles faraday response' do
class FaradayErrorWithResponseObject < Faraday::ClientError
def response
Faraday::Response.new(status: 500)
end
undef response_status
end
WebMock.stub_request(:get, 'http://example.com')
.to_raise(FaradayErrorWithResponseObject.new)

with_agent do
begin
ElasticAPM.with_transaction 'Faraday Middleware test' do
client.get('http://example.com')
end
rescue Faraday::ClientError
end
end
span, = @intercepted.spans

http = span.context.http
expect(http.status_code).to eq('500')
end

it 'handles faraday response hash' do
class FaradayErrorWithResponseHash < Faraday::ClientError
def response
{ status: 500 }
end
undef response_status
end
WebMock.stub_request(:get, 'http://example.com')
.to_raise(FaradayErrorWithResponseHash.new)

with_agent do
begin
ElasticAPM.with_transaction 'Faraday Middleware test' do
client.get('http://example.com')
end
rescue Faraday::ClientError
end
end
span, = @intercepted.spans

http = span.context.http
expect(http.status_code).to eq('500')
end

it 'does not raise error when response is string' do
class FaradayErrorWithResponseString < Faraday::ClientError
def response
'whatever'
end
undef response_status
end
WebMock.stub_request(:get, 'http://example.com')
.to_raise(FaradayErrorWithResponseString.new)

with_agent do
begin
ElasticAPM.with_transaction 'Faraday Middleware test' do
client.get('http://example.com')
end
rescue Faraday::ClientError
end
end
span, = @intercepted.spans

http = span.context.http
expect(http.status_code).to be nil
end
end
end
end

0 comments on commit 45deb8a

Please sign in to comment.