Skip to content

Commit

Permalink
Don't report network errors to Sentry (#2178)
Browse files Browse the repository at this point in the history
  • Loading branch information
natikgadzhi committed Nov 27, 2023
1 parent c92b0c8 commit 4c8110c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## Unreleased

### Features

### Bug Fixes

- Network errors raised in `Sentry::HTTPTransport` will no longer be reported to Sentry [#2178](https://github.com/getsentry/sentry-ruby/pull/2178)

## 5.14.0

### Features
Expand Down
14 changes: 12 additions & 2 deletions sentry-ruby/lib/sentry/transport/http_transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ class HTTPTransport < Transport
RATE_LIMIT_HEADER = "x-sentry-rate-limits"
USER_AGENT = "sentry-ruby/#{Sentry::VERSION}"

# The list of errors ::Net::HTTP is known to raise
# See https://github.com/ruby/ruby/blob/b0c639f249165d759596f9579fa985cb30533de6/lib/bundler/fetcher.rb#L281-L286
HTTP_ERRORS = [
Timeout::Error, EOFError, SocketError, Errno::ENETDOWN, Errno::ENETUNREACH,
Errno::EINVAL, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EAGAIN,
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError,
Zlib::BufError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED
].freeze


def initialize(*args)
super
@endpoint = @dsn.envelope_endpoint
Expand Down Expand Up @@ -58,8 +68,8 @@ def send_data(data)

raise Sentry::ExternalError, error_info
end
rescue SocketError => e
raise Sentry::ExternalError.new(e.message)
rescue SocketError, *HTTP_ERRORS => e
raise Sentry::ExternalError.new(e&.message)
end

private
Expand Down
29 changes: 26 additions & 3 deletions sentry-ruby/spec/sentry/transport/http_transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,34 @@
end
end

describe "failed to perform the network request" do
it "does not report Net::HTTP errors to Sentry" do
allow(::Net::HTTP).to receive(:new).and_raise(Errno::ECONNREFUSED)
expect do
subject.send_data(data)
end.to raise_error(Sentry::ExternalError)
end

it "does not report SocketError errors to Sentry" do
allow(::Net::HTTP).to receive(:new).and_raise(SocketError.new("socket error"))
expect do
subject.send_data(data)
end.to raise_error(Sentry::ExternalError)
end

it "reports other errors to Sentry if they are not recognized" do
allow(::Net::HTTP).to receive(:new).and_raise(StandardError.new("Booboo"))
expect do
subject.send_data(data)
end.to raise_error(StandardError, /Booboo/)
end
end

describe "failed request handling" do
context "receive 4xx responses" do
let(:fake_response) { build_fake_response("404") }

it 'raises an error' do
it "raises an error" do
stub_request(fake_response)

expect { subject.send_data(data) }.to raise_error(Sentry::ExternalError, /the server responded with status 404/)
Expand All @@ -279,7 +302,7 @@
context "receive 5xx responses" do
let(:fake_response) { build_fake_response("500") }

it 'raises an error' do
it "raises an error" do
stub_request(fake_response)

expect { subject.send_data(data) }.to raise_error(Sentry::ExternalError, /the server responded with status 500/)
Expand All @@ -291,7 +314,7 @@
build_fake_response("500", headers: { 'x-sentry-error' => 'error_in_header' })
end

it 'raises an error with header' do
it "raises an error with header" do
stub_request(error_response)

expect { subject.send_data(data) }.to raise_error(Sentry::ExternalError, /error_in_header/)
Expand Down

0 comments on commit 4c8110c

Please sign in to comment.