Mailgun: improve API error messages

This commit is contained in:
medmunds
2021-01-26 16:29:14 -08:00
committed by Mike Edmunds
parent 2196ab8330
commit d33c9ea4ed
6 changed files with 95 additions and 2 deletions

View File

@@ -47,6 +47,30 @@ class EmailBackend(AnymailRequestsBackend):
def build_message_payload(self, message, defaults):
return MailgunPayload(message, defaults, self)
def raise_for_status(self, response, payload, message):
# Mailgun issues a terse 404 for unrecognized sender domains.
# Add some context:
if response.status_code == 404 and "Domain not found" in response.text:
raise AnymailRequestsAPIError(
"Unknown sender domain {sender_domain!r}.\n"
"Check the domain is verified with Mailgun, and that the ANYMAIL"
" MAILGUN_API_URL setting {api_url!r} is the correct region.".format(
sender_domain=payload.sender_domain, api_url=self.api_url),
email_message=message, payload=payload,
response=response, backend=self)
super().raise_for_status(response, payload, message)
# Mailgun issues a cryptic "Mailgun Magnificent API" success response
# for invalid API endpoints. Convert that to a useful error:
if response.status_code == 200 and "Mailgun Magnificent API" in response.text:
raise AnymailRequestsAPIError(
"Invalid Mailgun API endpoint %r.\n"
"Check your ANYMAIL MAILGUN_SENDER_DOMAIN"
" and MAILGUN_API_URL settings." % response.url,
email_message=message, payload=payload,
response=response, backend=self)
def parse_recipient_status(self, response, payload, message):
# The *only* 200 response from Mailgun seems to be:
# {

View File

@@ -352,6 +352,15 @@ class MailgunInboundWebhookView(MailgunBaseWebhookView):
"You seem to have set Mailgun's *%s tracking* webhook "
"to Anymail's Mailgun *inbound* webhook URL." % request.POST['event'])
if 'attachments' in request.POST:
# Inbound route used store() rather than forward().
# ("attachments" seems to be the only POST param that differs between
# store and forward; Anymail could support store by handling the JSON
# attachments param in message_from_mailgun_parsed.)
raise AnymailConfigurationError(
"You seem to have configured Mailgun's receiving route using the store()"
" action. Anymail's inbound webhook requires the forward() action.")
if 'body-mime' in request.POST:
# Raw-MIME
message = AnymailInboundMessage.parse_raw_mime(request.POST['body-mime'])