Skip to content

Commit 497c281

Browse files
authored
Merge pull request coinbase#86 from JohnLZeller/zeller/issue-44-fix
Handle 502 HTTP error codes and absent JSON
2 parents 29fe98a + 88a9934 commit 497c281

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

coinbase/wallet/error.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from __future__ import print_function
55
from __future__ import unicode_literals
66

7+
from requests.compat import json
8+
79

810
class CoinbaseError(Exception):
911
"""Base error class for all exceptions raised in this library.
@@ -83,6 +85,10 @@ class InternalServerError(APIError):
8385
pass
8486

8587

88+
class BadGatewayError(APIError):
89+
pass
90+
91+
8692
class ServiceUnavailableError(APIError):
8793
pass
8894

@@ -91,7 +97,10 @@ def build_api_error(response, blob=None):
9197
"""Helper method for creating errors and attaching HTTP response/request
9298
details to them.
9399
"""
94-
blob = blob or response.json()
100+
try:
101+
blob = blob or response.json()
102+
except json.JSONDecodeError:
103+
blob = {}
95104
error_list = blob.get('errors', None)
96105
error = (error_list[0] if error_list else {})
97106
if error:
@@ -135,5 +144,6 @@ def build_api_error(response, blob=None):
135144
422: ValidationError,
136145
429: RateLimitExceededError,
137146
500: InternalServerError,
147+
502: BadGatewayError,
138148
503: ServiceUnavailableError,
139149
}

0 commit comments

Comments
 (0)