| 1 |
import json |
| 2 |
|
| 3 |
from ..exceptions import SparkPostAPIException as RequestsSparkPostAPIException |
| 4 |
|
| 5 |
|
| 6 |
class SparkPostAPIException(RequestsSparkPostAPIException): |
| 7 |
def __init__(self, response, *args, **kwargs): |
| 8 |
errors = None |
| 9 |
# noinspection PyBroadException |
| 10 |
try: |
| 11 |
data = json.loads(response.body.decode("utf-8")) |
| 12 |
if data: |
| 13 |
errors = data['errors'] |
| 14 |
errors = [e['message'] + ': ' + e.get('description', '') |
| 15 |
for e in errors] |
| 16 |
# TODO: select exception to catch here |
| 17 |
except: # noqa: E722 |
| 18 |
pass |
| 19 |
if not errors: |
| 20 |
errors = [response.body.decode("utf-8") or ""] |
| 21 |
self.status = response.code |
| 22 |
self.response = response |
| 23 |
self.errors = errors |
| 24 |
message = """Call to {uri} returned {status_code}, errors: |
| 25 |
|
| 26 |
{errors} |
| 27 |
""".format( |
| 28 |
uri=response.effective_url, |
| 29 |
status_code=response.code, |
| 30 |
errors='\n'.join(errors) |
| 31 |
) |
| 32 |
super(RequestsSparkPostAPIException, self).__init__(message, *args, |
| 33 |
**kwargs) |