Skip to content

Commit

Permalink
Merge pull request #382 from stripe/ob-idempotency-error
Browse files Browse the repository at this point in the history
Introduce new `IdempotencyError` type
  • Loading branch information
brandur-stripe authored Dec 21, 2017
2 parents be398eb + d3efa4c commit 8cc7eb5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions stripe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
PermissionError,
RateLimitError,
CardError,
IdempotencyError,
InvalidRequestError,
SignatureVerificationError,
StripeError)
Expand Down
10 changes: 7 additions & 3 deletions stripe/api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,13 @@ def specific_api_error(self, rbody, rcode, resp, rheaders, error_data):
return error.RateLimitError(
error_data.get('message'), rbody, rcode, resp, rheaders)
elif rcode in [400, 404]:
return error.InvalidRequestError(
error_data.get('message'), error_data.get('param'),
error_data.get('code'), rbody, rcode, resp, rheaders)
if error_data.get('type') == "idempotency_error":
return error.IdempotencyError(
error_data.get('message'), rbody, rcode, resp, rheaders)
else:
return error.InvalidRequestError(
error_data.get('message'), error_data.get('param'),
error_data.get('code'), rbody, rcode, resp, rheaders)
elif rcode == 401:
return error.AuthenticationError(
error_data.get('message'), rbody, rcode, resp, rheaders)
Expand Down
4 changes: 4 additions & 0 deletions stripe/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def __init__(self, message, param, code, http_body=None,
self.code = code


class IdempotencyError(StripeError):
pass


class InvalidRequestError(StripeError):

def __init__(self, message, param, code=None, http_body=None,
Expand Down
16 changes: 15 additions & 1 deletion tests/test_api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,27 @@ def test_fails_without_api_key(self):
self.requestor.request,
'get', self.valid_path, {})

def test_not_found(self):
def test_invalid_request_error_404(self):
self.mock_response('{"error": {}}', 404)

self.assertRaises(stripe.error.InvalidRequestError,
self.requestor.request,
'get', self.valid_path, {})

def test_invalid_request_error_400(self):
self.mock_response('{"error": {}}', 400)

self.assertRaises(stripe.error.InvalidRequestError,
self.requestor.request,
'get', self.valid_path, {})

def test_idempotency_error(self):
self.mock_response('{"error": {"type": "idempotency_error"}}', 400)

self.assertRaises(stripe.error.IdempotencyError,
self.requestor.request,
'get', self.valid_path, {})

def test_authentication_error(self):
self.mock_response('{"error": {}}', 401)

Expand Down

0 comments on commit 8cc7eb5

Please sign in to comment.