Skip to content

Commit 76d7bae

Browse files
authored
Merge branch 'master' into byk/feat/opportunistic-brotli
2 parents 9ca69c2 + 4f79aec commit 76d7bae

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

sentry_sdk/integrations/django/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,18 @@ def _set_db_data(span, cursor_or_db):
717717
connection_params = cursor_or_db.connection.get_dsn_parameters()
718718
else:
719719
try:
720-
# psycopg3
721-
connection_params = cursor_or_db.connection.info.get_parameters()
720+
# psycopg3, only extract needed params as get_parameters
721+
# can be slow because of the additional logic to filter out default
722+
# values
723+
connection_params = {
724+
"dbname": cursor_or_db.connection.info.dbname,
725+
"port": cursor_or_db.connection.info.port,
726+
}
727+
# PGhost returns host or base dir of UNIX socket as an absolute path
728+
# starting with /, use it only when it contains host
729+
pg_host = cursor_or_db.connection.info.host
730+
if pg_host and not pg_host.startswith("/"):
731+
connection_params["host"] = pg_host
722732
except Exception:
723733
connection_params = db.get_connection_params()
724734

sentry_sdk/utils.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,11 +713,21 @@ def get_errno(exc_value):
713713

714714
def get_error_message(exc_value):
715715
# type: (Optional[BaseException]) -> str
716-
return (
716+
message = (
717717
getattr(exc_value, "message", "")
718718
or getattr(exc_value, "detail", "")
719719
or safe_str(exc_value)
720-
)
720+
) # type: str
721+
722+
# __notes__ should be a list of strings when notes are added
723+
# via add_note, but can be anything else if __notes__ is set
724+
# directly. We only support strings in __notes__, since that
725+
# is the correct use.
726+
notes = getattr(exc_value, "__notes__", None) # type: object
727+
if isinstance(notes, list) and len(notes) > 0:
728+
message += "\n" + "\n".join(note for note in notes if isinstance(note, str))
729+
730+
return message
721731

722732

723733
def single_exception_from_error_tuple(

tests/test_basics.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,3 +999,46 @@ def test_hub_current_deprecation_warning():
999999
def test_hub_main_deprecation_warnings():
10001000
with pytest.warns(sentry_sdk.hub.SentryHubDeprecationWarning):
10011001
Hub.main
1002+
1003+
1004+
@pytest.mark.skipif(sys.version_info < (3, 11), reason="add_note() not supported")
1005+
def test_notes(sentry_init, capture_events):
1006+
sentry_init()
1007+
events = capture_events()
1008+
try:
1009+
e = ValueError("aha!")
1010+
e.add_note("Test 123")
1011+
e.add_note("another note")
1012+
raise e
1013+
except Exception:
1014+
capture_exception()
1015+
1016+
(event,) = events
1017+
1018+
assert event["exception"]["values"][0]["value"] == "aha!\nTest 123\nanother note"
1019+
1020+
1021+
@pytest.mark.skipif(sys.version_info < (3, 11), reason="add_note() not supported")
1022+
def test_notes_safe_str(sentry_init, capture_events):
1023+
class Note2:
1024+
def __repr__(self):
1025+
raise TypeError
1026+
1027+
def __str__(self):
1028+
raise TypeError
1029+
1030+
sentry_init()
1031+
events = capture_events()
1032+
try:
1033+
e = ValueError("aha!")
1034+
e.add_note("note 1")
1035+
e.__notes__.append(Note2()) # type: ignore
1036+
e.add_note("note 3")
1037+
e.__notes__.append(2) # type: ignore
1038+
raise e
1039+
except Exception:
1040+
capture_exception()
1041+
1042+
(event,) = events
1043+
1044+
assert event["exception"]["values"][0]["value"] == "aha!\nnote 1\nnote 3"

0 commit comments

Comments
 (0)