Raise Exception if letter PDF not in S3

Previously, the function would just return a presumed filename. Now that
it actually checks s3, if the file doesn't exist it'll raise an
exception. By default that's a StopIteration at the end of the bucket
iterator, which isn't ideal as this will get supressed if the function
is called within a generator loop further up or anything.

There are a couple of places where we expect the file may not exist, so
we define a custom exception to rescue specifically here. I did consider
subclassing boto's ClientError, but this wasn't straightforward as the
constructor expects to know the operation that failed, which for me is a
signal that it's not an appropriate (re-)use of the class.
This commit is contained in:
Leo Hemsted
2021-03-08 18:09:16 +00:00
committed by Ben Thorner
parent b43a367d5f
commit 6784ae62a6
4 changed files with 32 additions and 4 deletions

View File

@@ -27,7 +27,7 @@ from app.clients.sms.firetext import (
get_message_status_and_reason_from_firetext_code,
)
from app.dao.dao_utils import transactional
from app.letters.utils import find_letter_pdf_filename
from app.letters.utils import LetterPDFNotFound, find_letter_pdf_filename
from app.models import (
EMAIL_TYPE,
KEY_TYPE_NORMAL,
@@ -453,7 +453,13 @@ def _delete_letters_from_s3(
Notification.status.in_(NOTIFICATION_STATUS_TYPES_COMPLETED)
).limit(query_limit).all()
for letter in letters_to_delete_from_s3:
prefix = find_letter_pdf_filename(letter)
try:
prefix = find_letter_pdf_filename(letter)
except LetterPDFNotFound:
current_app.logger.exception(
"Could not delete S3 object for letter: {}".format(letter.id))
continue
s3_objects = get_s3_bucket_objects(bucket_name=bucket_name, subfolder=prefix)
for s3_object in s3_objects:
try: