mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 03:41:05 -05:00
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:
@@ -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"""
|
||||
|
||||
Reference in New Issue
Block a user