Skip to content

Commit

Permalink
Add repr methods for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed committed Jun 18, 2018
1 parent 2f5fe5a commit 18a589d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
24 changes: 22 additions & 2 deletions stripe/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ def __str__(self):
def user_message(self):
return self._message

def __repr__(self):
return '%s(message=%r, http_status=%r, request_id=%r)' % (
self.__class__.__name__,
self._message,
self.http_status,
self.request_id)


class APIError(StripeError):
pass
Expand All @@ -49,7 +56,20 @@ class APIConnectionError(StripeError):
pass


class CardError(StripeError):
class StripeErrorWithParamCode(StripeError):

def __repr__(self):
return ('%s(message=%r, param=%r, code=%r, http_status=%r, '
'request_id=%r)' % (
self.__class__.__name__,
self._message,
self.param,
self.code,
self.http_status,
self.request_id))


class CardError(StripeErrorWithParamCode):

def __init__(self, message, param, code, http_body=None,
http_status=None, json_body=None, headers=None):
Expand All @@ -63,7 +83,7 @@ class IdempotencyError(StripeError):
pass


class InvalidRequestError(StripeError):
class InvalidRequestError(StripeErrorWithParamCode):

def __init__(self, message, param, code=None, http_body=None,
http_status=None, json_body=None, headers=None):
Expand Down
28 changes: 27 additions & 1 deletion tests/test_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import absolute_import, division, print_function

from stripe import six, StripeError
from stripe import CardError, StripeError, six
from tests.helper import StripeTestCase


Expand Down Expand Up @@ -39,3 +39,29 @@ def test_formatting_with_message_none_and_request_id_none(self):
self.assertEqual('<empty message>', str(err))
else:
self.assertEqual('<empty message>', str(err))

def test_repr(self):
err = StripeError(u'öre', headers={'request-id': '123'})
if six.PY2:
self.assertEquals(repr(err), (
"StripeError(message=u'\\xf6re', http_status=None, "
"request_id='123')"))
else:
self.assertEquals(repr(err), (
"StripeError(message='öre', http_status=None, "
"request_id='123')"))


class StripeErrorWithParamCodeTests(StripeTestCase):

def test_repr(self):
err = CardError(u'öre', param='cparam', code='ccode', http_status=403,
headers={'request-id': '123'})
if six.PY2:
self.assertEquals(repr(err), (
"CardError(message=u'\\xf6re', param='cparam', code='ccode', "
"http_status=403, request_id='123')"))
else:
self.assertEquals(repr(err), (
"CardError(message='öre', param='cparam', code='ccode', "
"http_status=403, request_id='123')"))

0 comments on commit 18a589d

Please sign in to comment.