diff --git a/sentry_sdk/envelope.py b/sentry_sdk/envelope.py index 44cce52410..6bb1eb22c7 100644 --- a/sentry_sdk/envelope.py +++ b/sentry_sdk/envelope.py @@ -189,9 +189,7 @@ def get_bytes(self): self.bytes = f.read() elif self.json is not None: self.bytes = json_dumps(self.json) - else: - self.bytes = b"" - return self.bytes + return self.bytes or b"" @property def inferred_content_type(self): diff --git a/tests/test_basics.py b/tests/test_basics.py index 3a801c5785..e1e84340a5 100644 --- a/tests/test_basics.py +++ b/tests/test_basics.py @@ -459,6 +459,22 @@ def test_attachments(sentry_init, capture_envelopes): assert pyfile.payload.get_bytes() == f.read() +@pytest.mark.tests_internal_exceptions +def test_attachments_graceful_failure( + sentry_init, capture_envelopes, internal_exceptions +): + sentry_init() + envelopes = capture_envelopes() + + with configure_scope() as scope: + scope.add_attachment(path="non_existent") + capture_exception(ValueError()) + + (envelope,) = envelopes + assert len(envelope.items) == 2 + assert envelope.items[1].payload.get_bytes() == b"" + + def test_integration_scoping(sentry_init, capture_events): logger = logging.getLogger("test_basics")