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

@@ -26,6 +26,7 @@ from app.dao.templates_dao import dao_get_template_by_id
from app.errors import VirusScanError
from app.exceptions import NotificationTechnicalFailureException
from app.letters.utils import (
LetterPDFNotFound,
ScanErrorType,
find_letter_pdf_filename,
generate_letter_pdf_filename,
@@ -243,7 +244,7 @@ def get_key_and_size_of_letters_to_be_sent_to_print(print_run_deadline, postage)
"Size": letter_head['ContentLength'],
"ServiceId": str(letter.service_id)
}
except BotoClientError as e:
except (BotoClientError, LetterPDFNotFound) as e:
current_app.logger.exception(
f"Error getting letter from bucket for notification: {letter.id} with reference: {letter.reference}", e)