Skip to content

Commit 0e4d103

Browse files
authored
Gracefully fail attachment path not found case (#3337)
1 parent 3ecdf89 commit 0e4d103

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

sentry_sdk/envelope.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ def get_bytes(self):
189189
self.bytes = f.read()
190190
elif self.json is not None:
191191
self.bytes = json_dumps(self.json)
192-
else:
193-
self.bytes = b""
194-
return self.bytes
192+
return self.bytes or b""
195193

196194
@property
197195
def inferred_content_type(self):

tests/test_basics.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,22 @@ def test_attachments(sentry_init, capture_envelopes):
459459
assert pyfile.payload.get_bytes() == f.read()
460460

461461

462+
@pytest.mark.tests_internal_exceptions
463+
def test_attachments_graceful_failure(
464+
sentry_init, capture_envelopes, internal_exceptions
465+
):
466+
sentry_init()
467+
envelopes = capture_envelopes()
468+
469+
with configure_scope() as scope:
470+
scope.add_attachment(path="non_existent")
471+
capture_exception(ValueError())
472+
473+
(envelope,) = envelopes
474+
assert len(envelope.items) == 2
475+
assert envelope.items[1].payload.get_bytes() == b""
476+
477+
462478
def test_integration_scoping(sentry_init, capture_events):
463479
logger = logging.getLogger("test_basics")
464480

0 commit comments

Comments
 (0)