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:
Mike Edmunds
2023-05-02 12:38:18 -07:00
parent 1ba26e1be3
commit 7d993ee610
7 changed files with 75 additions and 4 deletions

View File

@@ -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