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