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

@@ -12,7 +12,7 @@ from django.test import SimpleTestCase
from django.test.utils import override_settings
from django.utils.timezone import get_fixed_timezone, override as override_current_timezone
from anymail.exceptions import AnymailAPIError, AnymailUnsupportedFeature
from anymail.exceptions import AnymailAPIError, AnymailRequestsAPIError, AnymailUnsupportedFeature
from anymail.message import attach_inline_image_file
from .mock_requests_backend import RequestsBackendMockAPITestCase, SessionSharingTestCasesMixin
@@ -258,6 +258,20 @@ class MailgunBackendStandardEmailTests(MailgunBackendMockAPITestCase):
with self.assertRaises(AnymailAPIError):
self.message.send()
def test_requests_exception(self):
"""Exception during API call should be AnymailAPIError"""
# (The post itself raises an error -- different from returning a failure response)
from requests.exceptions import SSLError # a low-level requests exception
self.mock_request.side_effect = SSLError("Something bad")
with self.assertRaisesMessage(AnymailRequestsAPIError, "Something bad") as cm:
self.message.send()
self.assertIsInstance(cm.exception, SSLError) # also retains specific requests exception class
# Make sure fail_silently is respected
self.mock_request.side_effect = SSLError("Something bad")
sent = mail.send_mail('Subject', 'Body', 'from@example.com', ['to@example.com'], fail_silently=True)
self.assertEqual(sent, 0)
class MailgunBackendAnymailFeatureTests(MailgunBackendMockAPITestCase):
"""Test backend support for Anymail added features"""