Skip to content

Commit 4c8110c

Browse files
authored
Don't report network errors to Sentry (#2178)
1 parent c92b0c8 commit 4c8110c

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## Unreleased
2+
3+
### Features
4+
5+
### Bug Fixes
6+
7+
- Network errors raised in `Sentry::HTTPTransport` will no longer be reported to Sentry [#2178](https://github.com/getsentry/sentry-ruby/pull/2178)
8+
19
## 5.14.0
210

311
### Features

sentry-ruby/lib/sentry/transport/http_transport.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ class HTTPTransport < Transport
1414
RATE_LIMIT_HEADER = "x-sentry-rate-limits"
1515
USER_AGENT = "sentry-ruby/#{Sentry::VERSION}"
1616

17+
# The list of errors ::Net::HTTP is known to raise
18+
# See https://github.com/ruby/ruby/blob/b0c639f249165d759596f9579fa985cb30533de6/lib/bundler/fetcher.rb#L281-L286
19+
HTTP_ERRORS = [
20+
Timeout::Error, EOFError, SocketError, Errno::ENETDOWN, Errno::ENETUNREACH,
21+
Errno::EINVAL, Errno::ECONNRESET, Errno::ETIMEDOUT, Errno::EAGAIN,
22+
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError,
23+
Zlib::BufError, Errno::EHOSTUNREACH, Errno::ECONNREFUSED
24+
].freeze
25+
26+
1727
def initialize(*args)
1828
super
1929
@endpoint = @dsn.envelope_endpoint
@@ -58,8 +68,8 @@ def send_data(data)
5868

5969
raise Sentry::ExternalError, error_info
6070
end
61-
rescue SocketError => e
62-
raise Sentry::ExternalError.new(e.message)
71+
rescue SocketError, *HTTP_ERRORS => e
72+
raise Sentry::ExternalError.new(e&.message)
6373
end
6474

6575
private

sentry-ruby/spec/sentry/transport/http_transport_spec.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,34 @@
265265
end
266266
end
267267

268+
describe "failed to perform the network request" do
269+
it "does not report Net::HTTP errors to Sentry" do
270+
allow(::Net::HTTP).to receive(:new).and_raise(Errno::ECONNREFUSED)
271+
expect do
272+
subject.send_data(data)
273+
end.to raise_error(Sentry::ExternalError)
274+
end
275+
276+
it "does not report SocketError errors to Sentry" do
277+
allow(::Net::HTTP).to receive(:new).and_raise(SocketError.new("socket error"))
278+
expect do
279+
subject.send_data(data)
280+
end.to raise_error(Sentry::ExternalError)
281+
end
282+
283+
it "reports other errors to Sentry if they are not recognized" do
284+
allow(::Net::HTTP).to receive(:new).and_raise(StandardError.new("Booboo"))
285+
expect do
286+
subject.send_data(data)
287+
end.to raise_error(StandardError, /Booboo/)
288+
end
289+
end
290+
268291
describe "failed request handling" do
269292
context "receive 4xx responses" do
270293
let(:fake_response) { build_fake_response("404") }
271294

272-
it 'raises an error' do
295+
it "raises an error" do
273296
stub_request(fake_response)
274297

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

282-
it 'raises an error' do
305+
it "raises an error" do
283306
stub_request(fake_response)
284307

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

294-
it 'raises an error with header' do
317+
it "raises an error with header" do
295318
stub_request(error_response)
296319

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

0 commit comments

Comments
 (0)