Skip to content

Commit 2f14816

Browse files
nicolassanmarantonpirkersentrivanagoharShoukat
authored
fix: Exceptions include detail property for their value (#2193)
--------- Co-authored-by: Anton Pirker <anton.pirker@sentry.io> Co-authored-by: Ivana Kellyerova <ivana.kellyerova@sentry.io> Co-authored-by: Gohar Shoukat <25367760+goharShoukat@users.noreply.github.com>
1 parent 6bea3e8 commit 2f14816

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

sentry_sdk/utils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,15 @@ def get_errno(exc_value):
681681
return getattr(exc_value, "errno", None)
682682

683683

684+
def get_error_message(exc_value):
685+
# type: (Optional[BaseException]) -> str
686+
return (
687+
getattr(exc_value, "message", "")
688+
or getattr(exc_value, "detail", "")
689+
or safe_str(exc_value)
690+
)
691+
692+
684693
def single_exception_from_error_tuple(
685694
exc_type, # type: Optional[type]
686695
exc_value, # type: Optional[BaseException]
@@ -734,7 +743,7 @@ def single_exception_from_error_tuple(
734743

735744
exception_value["module"] = get_type_module(exc_type)
736745
exception_value["type"] = get_type_name(exc_type)
737-
exception_value["value"] = getattr(exc_value, "message", safe_str(exc_value))
746+
exception_value["value"] = get_error_message(exc_value)
738747

739748
if client_options is None:
740749
include_local_variables = True

tests/test_utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
from sentry_sdk.utils import (
66
Components,
7+
get_error_message,
78
is_valid_sample_rate,
89
logger,
910
match_regex_list,
1011
parse_url,
1112
parse_version,
13+
safe_str,
1214
sanitize_url,
1315
serialize_frame,
1416
)
@@ -423,3 +425,22 @@ def test_match_regex_list(item, regex_list, expected_result):
423425
)
424426
def test_parse_version(version, expected_result):
425427
assert parse_version(version) == expected_result
428+
429+
430+
@pytest.mark.parametrize(
431+
"error,expected_result",
432+
[
433+
["", lambda x: safe_str(x)],
434+
["some-string", lambda _: "some-string"],
435+
],
436+
)
437+
def test_get_error_message(error, expected_result):
438+
with pytest.raises(BaseException) as exc_value:
439+
exc_value.message = error
440+
raise Exception
441+
assert get_error_message(exc_value) == expected_result(exc_value)
442+
443+
with pytest.raises(BaseException) as exc_value:
444+
exc_value.detail = error
445+
raise Exception
446+
assert get_error_message(exc_value) == expected_result(exc_value)

0 commit comments

Comments
 (0)