Skip to content

Commit 8fe7c84

Browse files
author
Rich Leland
committed
Tweak SparkPostAPIException error handling
1 parent cc577cc commit 8fe7c84

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

sparkpost/exceptions.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ def __init__(self, response, *args, **kwargs):
88
errors = None
99
try:
1010
errors = response.json()['errors']
11-
errors = [e['message'] +
12-
' Code: ' + e.get('code', '') +
13-
' Description: ' + e.get('description', '') +
14-
'\n'
11+
error_template = "{message} Code: {code} Description: {desc} \n"
12+
errors = [error_template.format(message=e['message'],
13+
code=e.get('code', 'none'),
14+
desc=e.get('description', 'none'))
1515
for e in errors]
16-
except:
17-
pass
18-
if not errors:
16+
except ValueError:
1917
errors = [response.text or ""]
2018
self.status = response.status_code
2119
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)