From 53129b2197b2b43d4c20a37e3d4c598445422b53 Mon Sep 17 00:00:00 2001 From: Cenk Alti Date: Thu, 1 Mar 2018 01:02:33 +0300 Subject: fix raven.utils.json.dumps exception --- raven/utils/json.py | 5 ++++- tests/utils/json/tests.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/raven/utils/json.py b/raven/utils/json.py index afbd386..a727f72 100644 --- a/raven/utils/json.py +++ b/raven/utils/json.py @@ -35,7 +35,10 @@ class BetterJSONEncoder(json.JSONEncoder): try: return super(BetterJSONEncoder, self).default(obj) except TypeError: - return repr(obj) + try: + return repr(obj) + except Exception: + return object.__repr__(obj) return encoder(obj) diff --git a/tests/utils/json/tests.py b/tests/utils/json/tests.py index ca552b0..d85bc62 100644 --- a/tests/utils/json/tests.py +++ b/tests/utils/json/tests.py @@ -34,6 +34,17 @@ class JSONTest(TestCase): obj = Unknown() assert json.dumps(obj) == '"Unknown object"' + def test_unknown_type_with_repr_error(self): + + class Unknown(object): + def __repr__(self): + raise Exception + + obj = Unknown() + s = json.dumps(obj) + assert isinstance(s, str) + assert 'Unknown object at 0x' in s + def test_decimal(self): d = {'decimal': Decimal('123.45')} assert json.dumps(d) == '{"decimal": "Decimal(\'123.45\')"}' -- cgit v1.2.1