Skip to content

Commit f3b27cc

Browse files
authored
Merge pull request #135 from SparkPost/issue-134
Tweaks to exception handling, refs #134.
2 parents cc577cc + f343c21 commit f3b27cc

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

sparkpost/exceptions.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ class SparkPostException(Exception):
55
class SparkPostAPIException(SparkPostException):
66
"Handle 4xx and 5xx errors from the SparkPost API"
77
def __init__(self, response, *args, **kwargs):
8-
errors = None
98
try:
109
errors = response.json()['errors']
11-
errors = [e['message'] +
12-
' Code: ' + e.get('code', '') +
13-
' Description: ' + e.get('description', '') +
14-
'\n'
10+
error_template = "{message} Code: {code} Description: {desc} \n"
11+
errors = [error_template.format(message=e.get('message', ''),
12+
code=e.get('code', 'none'),
13+
desc=e.get('description', 'none'))
1514
for e in errors]
1615
except:
17-
pass
18-
if not errors:
1916
errors = [response.text or ""]
2017
self.status = response.status_code
2118
self.response = response

test/test_base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ def test_fail_nojson_request():
9292
resource.request('GET', resource.uri)
9393

9494

95+
@responses.activate
96+
def test_fail_no_errors():
97+
responses.add(
98+
responses.GET,
99+
fake_uri,
100+
status=500,
101+
content_type='application/json',
102+
body='no errors'
103+
)
104+
resource = create_resource()
105+
with pytest.raises(SparkPostAPIException):
106+
resource.request('GET', resource.uri)
107+
108+
95109
def test_fail_get():
96110
resource = create_resource()
97111
with pytest.raises(NotImplementedError):

test/test_transmissions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def test_fail_delete():
333333
content_type='application/json',
334334
body="""
335335
{"errors": [{"message": "resource not found",
336-
"description": "Resource not found:transmission id foobar""}]}
336+
"description": "Resource not found:transmission id foobar"}]}
337337
"""
338338
)
339339
with pytest.raises(SparkPostAPIException):

0 commit comments

Comments
 (0)