Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,15 @@ def get_errno(exc_value):
return getattr(exc_value, "errno", None)


def get_error_message(exc_value):
# type: (Optional[BaseException]) -> str
return (
getattr(exc_value, "message", "")
or getattr(exc_value, "detail", "")
or safe_str(exc_value)
)


def single_exception_from_error_tuple(
exc_type, # type: Optional[type]
exc_value, # type: Optional[BaseException]
Expand Down Expand Up @@ -734,7 +743,7 @@ def single_exception_from_error_tuple(

exception_value["module"] = get_type_module(exc_type)
exception_value["type"] = get_type_name(exc_type)
exception_value["value"] = getattr(exc_value, "message", safe_str(exc_value))
exception_value["value"] = get_error_message(exc_value)

if client_options is None:
include_local_variables = True
Expand Down
21 changes: 21 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

from sentry_sdk.utils import (
Components,
get_error_message,
is_valid_sample_rate,
logger,
match_regex_list,
parse_url,
parse_version,
safe_str,
sanitize_url,
serialize_frame,
)
Expand Down Expand Up @@ -423,3 +425,22 @@ def test_match_regex_list(item, regex_list, expected_result):
)
def test_parse_version(version, expected_result):
assert parse_version(version) == expected_result


@pytest.mark.parametrize(
"error,expected_result",
[
["", lambda x: safe_str(x)],
["some-string", lambda _: "some-string"],
],
)
def test_get_error_message(error, expected_result):
with pytest.raises(BaseException) as exc_value:
exc_value.message = error
raise Exception
assert get_error_message(exc_value) == expected_result(exc_value)

with pytest.raises(BaseException) as exc_value:
exc_value.detail = error
raise Exception
assert get_error_message(exc_value) == expected_result(exc_value)