mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 03:41:05 -05:00
Fix fail_silently when session/client creation fails
Make sure backends actually fail silently when asked (rather than raising inaccurate errors suggesting coding problems). Fixes #308
This commit is contained in:
@@ -59,7 +59,7 @@ class EmailBackend(AnymailBaseBackend):
|
||||
self.client = boto3.session.Session(**self.session_params).client(
|
||||
"ses", **self.client_params
|
||||
)
|
||||
except BOTO_BASE_ERRORS:
|
||||
except Exception:
|
||||
if not self.fail_silently:
|
||||
raise
|
||||
else:
|
||||
@@ -71,6 +71,22 @@ class EmailBackend(AnymailBaseBackend):
|
||||
# self.client.close() # boto3 doesn't support (or require) client shutdown
|
||||
self.client = None
|
||||
|
||||
def _send(self, message):
|
||||
if self.client:
|
||||
return super()._send(message)
|
||||
elif self.fail_silently:
|
||||
# (Probably missing boto3 credentials in open().)
|
||||
return False
|
||||
else:
|
||||
class_name = self.__class__.__name__
|
||||
raise RuntimeError(
|
||||
"boto3 Session has not been opened in {class_name}._send. "
|
||||
"(This is either an implementation error in {class_name}, "
|
||||
"or you are incorrectly calling _send directly.)".format(
|
||||
class_name=class_name
|
||||
)
|
||||
)
|
||||
|
||||
def build_message_payload(self, message, defaults):
|
||||
# The SES SendRawEmail and SendBulkTemplatedEmail calls have
|
||||
# very different signatures, so use a custom payload for each
|
||||
|
||||
@@ -63,7 +63,7 @@ class EmailBackend(AnymailBaseBackend):
|
||||
self.client = boto3.session.Session(**self.session_params).client(
|
||||
"sesv2", **self.client_params
|
||||
)
|
||||
except BOTO_BASE_ERRORS:
|
||||
except Exception:
|
||||
if not self.fail_silently:
|
||||
raise
|
||||
else:
|
||||
@@ -75,6 +75,22 @@ class EmailBackend(AnymailBaseBackend):
|
||||
self.client.close()
|
||||
self.client = None
|
||||
|
||||
def _send(self, message):
|
||||
if self.client:
|
||||
return super()._send(message)
|
||||
elif self.fail_silently:
|
||||
# (Probably missing boto3 credentials in open().)
|
||||
return False
|
||||
else:
|
||||
class_name = self.__class__.__name__
|
||||
raise RuntimeError(
|
||||
"boto3 Session has not been opened in {class_name}._send. "
|
||||
"(This is either an implementation error in {class_name}, "
|
||||
"or you are incorrectly calling _send directly.)".format(
|
||||
class_name=class_name
|
||||
)
|
||||
)
|
||||
|
||||
def build_message_payload(self, message, defaults):
|
||||
if getattr(message, "template_id", UNSET) is not UNSET:
|
||||
# For simplicity, use SESv2 SendBulkEmail for all templated messages
|
||||
|
||||
@@ -47,7 +47,12 @@ class AnymailRequestsBackend(AnymailBaseBackend):
|
||||
self.session = None
|
||||
|
||||
def _send(self, message):
|
||||
if self.session is None:
|
||||
if self.session:
|
||||
return super()._send(message)
|
||||
elif self.fail_silently:
|
||||
# create_session failed
|
||||
return False
|
||||
else:
|
||||
class_name = self.__class__.__name__
|
||||
raise RuntimeError(
|
||||
"Session has not been opened in {class_name}._send. "
|
||||
@@ -56,7 +61,6 @@ class AnymailRequestsBackend(AnymailBaseBackend):
|
||||
class_name=class_name
|
||||
)
|
||||
)
|
||||
return super()._send(message)
|
||||
|
||||
def create_session(self):
|
||||
"""Create the requests.Session object used by this instance of the backend.
|
||||
|
||||
Reference in New Issue
Block a user