Upgrade requests exceptions to AnymailRequestsAPIError

Catch and re-raise requests.RequestException in
AnymailRequestsBackend.post_to_esp.

* AnymailRequestsAPIError is needed for proper
  fail_silently handling.
* Retain original requests exception type, to avoid
  breaking existing code that might look for specific
  requests exceptions.

Closes #16
This commit is contained in:
medmunds
2016-06-01 10:18:27 -07:00
parent 5d2bc66190
commit 1ea9ab6fee
3 changed files with 34 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
import json
from traceback import format_exception_only
from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation
from requests import HTTPError
@@ -18,11 +19,13 @@ class AnymailError(Exception):
status_code: HTTP status code of response to ESP send call
payload: data arg (*not* json-stringified) for the ESP send call
response: requests.Response from the send call
raised_from: original/wrapped Exception
"""
self.backend = kwargs.pop('backend', None)
self.email_message = kwargs.pop('email_message', None)
self.payload = kwargs.pop('payload', None)
self.status_code = kwargs.pop('status_code', None)
self.raised_from = kwargs.pop('raised_from', None)
if isinstance(self, HTTPError):
# must leave response in kwargs for HTTPError
self.response = kwargs.get('response', None)
@@ -33,6 +36,7 @@ class AnymailError(Exception):
def __str__(self):
parts = [
" ".join([str(arg) for arg in self.args]),
self.describe_raised_from(),
self.describe_send(),
self.describe_response(),
]
@@ -68,6 +72,12 @@ class AnymailError(Exception):
pass
return description
def describe_raised_from(self):
"""Return the original exception"""
if self.raised_from is None:
return None
return ''.join(format_exception_only(type(self.raised_from), self.raised_from)).strip()
class AnymailAPIError(AnymailError):
"""Exception for unsuccessful response from ESP's API."""