summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCenk Alti <cenkalti@gmail.com>2018-03-01 01:02:33 +0300
committerAshley Camba <ashwoods@gmail.com>2018-04-18 11:43:08 +0200
commit53129b2197b2b43d4c20a37e3d4c598445422b53 (patch)
treec0d7279026098b0095fd6db3e4c729d619957607
parentd75531762b1dab1585b80e93df8ee88acdd1cec8 (diff)
downloadraven-53129b2197b2b43d4c20a37e3d4c598445422b53.tar.gz
fix raven.utils.json.dumps exception
-rw-r--r--raven/utils/json.py5
-rw-r--r--tests/utils/json/tests.py11
2 files changed, 15 insertions, 1 deletions
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\')"}'