Skip to content

Commit cdfab0d

Browse files
authored
feat(serializer): Allow classes to short circuit serializer with sentry_repr (#1322)
1 parent 4ce0a1d commit cdfab0d

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

sentry_sdk/serializer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ def _serialize_node_impl(
281281
else:
282282
return obj
283283

284+
elif callable(getattr(obj, "sentry_repr", None)):
285+
return obj.sentry_repr()
286+
284287
elif isinstance(obj, datetime):
285288
return (
286289
text_type(format_timestamp(obj))

tests/test_serializer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,12 @@ def test_bytes_serialization_repr(message_normalizer):
6464
def test_serialize_sets(extra_normalizer):
6565
result = extra_normalizer({1, 2, 3})
6666
assert result == [1, 2, 3]
67+
68+
69+
def test_serialize_custom_mapping(extra_normalizer):
70+
class CustomReprDict(dict):
71+
def sentry_repr(self):
72+
return "custom!"
73+
74+
result = extra_normalizer(CustomReprDict(one=1, two=2))
75+
assert result == "custom!"

0 commit comments

Comments
 (0)