Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(serializer): Allow classes to short circuit serializer with sentry_repr #1322

Merged
merged 1 commit into from
Jan 26, 2022
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
3 changes: 3 additions & 0 deletions sentry_sdk/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ def _serialize_node_impl(
else:
return obj

elif callable(getattr(obj, "sentry_repr", None)):
return obj.sentry_repr()

elif isinstance(obj, datetime):
return (
text_type(format_timestamp(obj))
Expand Down
9 changes: 9 additions & 0 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,12 @@ def test_bytes_serialization_repr(message_normalizer):
def test_serialize_sets(extra_normalizer):
result = extra_normalizer({1, 2, 3})
assert result == [1, 2, 3]


def test_serialize_custom_mapping(extra_normalizer):
class CustomReprDict(dict):
def sentry_repr(self):
return "custom!"

result = extra_normalizer(CustomReprDict(one=1, two=2))
assert result == "custom!"