Skip to content

Commit 53129b2

Browse files
cenkaltiashwoods
authored andcommitted
fix raven.utils.json.dumps exception
1 parent d755317 commit 53129b2

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

raven/utils/json.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ def default(self, obj):
3535
try:
3636
return super(BetterJSONEncoder, self).default(obj)
3737
except TypeError:
38-
return repr(obj)
38+
try:
39+
return repr(obj)
40+
except Exception:
41+
return object.__repr__(obj)
3942
return encoder(obj)
4043

4144

tests/utils/json/tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ def __repr__(self):
3434
obj = Unknown()
3535
assert json.dumps(obj) == '"Unknown object"'
3636

37+
def test_unknown_type_with_repr_error(self):
38+
39+
class Unknown(object):
40+
def __repr__(self):
41+
raise Exception
42+
43+
obj = Unknown()
44+
s = json.dumps(obj)
45+
assert isinstance(s, str)
46+
assert 'Unknown object at 0x' in s
47+
3748
def test_decimal(self):
3849
d = {'decimal': Decimal('123.45')}
3950
assert json.dumps(d) == '{"decimal": "Decimal(\'123.45\')"}'

0 commit comments

Comments
 (0)