diff --git a/sparkpost/exceptions.py b/sparkpost/exceptions.py index 0fdb00d..3c2a9e9 100644 --- a/sparkpost/exceptions.py +++ b/sparkpost/exceptions.py @@ -5,17 +5,14 @@ class SparkPostException(Exception): class SparkPostAPIException(SparkPostException): "Handle 4xx and 5xx errors from the SparkPost API" def __init__(self, response, *args, **kwargs): - errors = None try: errors = response.json()['errors'] - errors = [e['message'] + - ' Code: ' + e.get('code', '') + - ' Description: ' + e.get('description', '') + - '\n' + error_template = "{message} Code: {code} Description: {desc} \n" + errors = [error_template.format(message=e.get('message', ''), + code=e.get('code', 'none'), + desc=e.get('description', 'none')) for e in errors] except: - pass - if not errors: errors = [response.text or ""] self.status = response.status_code self.response = response diff --git a/test/test_base.py b/test/test_base.py index 0bd59cd..aff6b79 100644 --- a/test/test_base.py +++ b/test/test_base.py @@ -92,6 +92,20 @@ def test_fail_nojson_request(): resource.request('GET', resource.uri) +@responses.activate +def test_fail_no_errors(): + responses.add( + responses.GET, + fake_uri, + status=500, + content_type='application/json', + body='no errors' + ) + resource = create_resource() + with pytest.raises(SparkPostAPIException): + resource.request('GET', resource.uri) + + def test_fail_get(): resource = create_resource() with pytest.raises(NotImplementedError): diff --git a/test/test_transmissions.py b/test/test_transmissions.py index c2d4e32..9847fb2 100644 --- a/test/test_transmissions.py +++ b/test/test_transmissions.py @@ -333,7 +333,7 @@ def test_fail_delete(): content_type='application/json', body=""" {"errors": [{"message": "resource not found", - "description": "Resource not found:transmission id foobar""}]} + "description": "Resource not found:transmission id foobar"}]} """ ) with pytest.raises(SparkPostAPIException):