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

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