diff --git a/stripe/error.py b/stripe/error.py index 0f456de37..11cf12d7c 100644 --- a/stripe/error.py +++ b/stripe/error.py @@ -59,7 +59,11 @@ def __repr__(self): ) def construct_error_object(self): - if self.json_body is None or "error" not in self.json_body: + if ( + self.json_body is None + or "error" not in self.json_body + or not isinstance(self.json_body["error"], dict) + ): return None return stripe.api_resources.error_object.ErrorObject.construct_from( diff --git a/tests/test_error.py b/tests/test_error.py index 148ead925..644c321b0 100644 --- a/tests/test_error.py +++ b/tests/test_error.py @@ -60,6 +60,10 @@ def test_error_object(self): assert err.error.code == "some_error" assert err.error.charge is None + def test_error_object_not_dict(self): + err = error.StripeError("message", json_body={"error": "not a dict"}) + assert err.error is None + class TestStripeErrorWithParamCode(object): def test_repr(self):