Raise error for invalid/rejected recipients

Raise new MandrillRecipientsRefused exception
when Mandrill returns 'reject' or 'invalid' status
for *all* recipients of a message.

(Similar to Django's SMTP email backend raising
SMTPRecipientsRefused.)

Add setting MANDRILL_IGNORE_RECIPIENT_STATUS
to override the new exception.

Trap JSON parsing errors in Mandrill API response,
and raise MandrillAPIError for them. (Helps with #93.)

Closes #80.
Closes #81.
This commit is contained in:
medmunds
2015-12-01 13:26:21 -08:00
parent 8433e6d660
commit d14b87c910
9 changed files with 204 additions and 37 deletions

View File

@@ -2,6 +2,23 @@ import json
from requests import HTTPError
def format_response(response):
"""Return a string-formatted version of response
Format json if available, else just return text.
Returns "" if neither json nor text available.
"""
try:
json_response = response.json()
return "\n" + json.dumps(json_response, indent=2)
except (AttributeError, KeyError, ValueError): # not JSON = ValueError
try:
return response.text
except AttributeError:
pass
return ""
class MandrillAPIError(HTTPError):
"""Exception for unsuccessful response from Mandrill API."""
def __init__(self, status_code, response=None, log_message=None, *args, **kwargs):
@@ -15,14 +32,21 @@ class MandrillAPIError(HTTPError):
if self.log_message:
message += "\n" + self.log_message
# Include the Mandrill response, nicely formatted, if possible
try:
json_response = self.response.json()
message += "\nMandrill response:\n" + json.dumps(json_response, indent=2)
except (AttributeError, KeyError, ValueError): # not JSON = ValueError
try:
message += "\nMandrill response: " + self.response.text
except AttributeError:
pass
if self.response is not None:
message += "\nMandrill response: " + format_response(self.response)
return message
class MandrillRecipientsRefused(IOError):
"""Exception for send where all recipients are invalid or rejected."""
def __init__(self, message, response=None, *args, **kwargs):
super(MandrillRecipientsRefused, self).__init__(message, *args, **kwargs)
self.response = response
def __str__(self):
message = self.args[0]
if self.response is not None:
message += "\nMandrill response: " + format_response(self.response)
return message