Catch error if letter does not exist on send

This repeats the pattern we already have for previewing a letter,
where we assume the error is because the notification has already
been sent and redirect the user to see it.

I've improved the original pattern a bit:

- I've DRYed-up the low-level boto code and moved the error handler
there so it can be reused.

- I've introduced a custom exception, which the calling code can
choose to log.

- I've introduced the moto library, which we use elsewhere, to make
it easier to test S3 code.

I've used an error level log when sending a notification - now that
we have a more descriptive log, we can verify the assumption is true
and then make an informed decision to downgrade the log.

In future we may want to merge this handler with the similar code
in utils [1], but we'll need to be careful as the utils handler is
superficial - it doesn't check the reason for the error.

[1]: bce0f4e596/notifications_utils/s3.py (L52)
This commit is contained in:
Ben Thorner
2022-02-18 14:37:09 +00:00
parent 73cc034676
commit 4fef2861c6
5 changed files with 103 additions and 24 deletions

View File

@@ -1,11 +1,16 @@
import json
import urllib
import botocore
from boto3 import resource
from flask import current_app
from notifications_utils.s3 import s3upload as utils_s3upload
class LetterNotFoundError(Exception):
pass
def get_transient_letter_file_location(service_id, upload_id):
return 'service-{}/{}.pdf'.format(service_id, upload_id)
@@ -69,19 +74,24 @@ class LetterMetadata:
return value
def get_letter_s3_object(service_id, file_id):
try:
file_location = get_transient_letter_file_location(service_id, file_id)
s3 = resource('s3')
return s3.Object(current_app.config['TRANSIENT_UPLOADED_LETTERS'], file_location).get()
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'NoSuchKey':
raise LetterNotFoundError(f'Letter not found for service {service_id} and file {file_id}')
raise
def get_letter_pdf_and_metadata(service_id, file_id):
file_location = get_transient_letter_file_location(service_id, file_id)
s3 = resource('s3')
s3_object = s3.Object(current_app.config['TRANSIENT_UPLOADED_LETTERS'], file_location).get()
s3_object = get_letter_s3_object(service_id, file_id)
pdf = s3_object['Body'].read()
return pdf, LetterMetadata(s3_object['Metadata'])
def get_letter_metadata(service_id, file_id):
file_location = get_transient_letter_file_location(service_id, file_id)
s3 = resource('s3')
s3_object = s3.Object(current_app.config['TRANSIENT_UPLOADED_LETTERS'], file_location).get()
s3_object = get_letter_s3_object(service_id, file_id)
return LetterMetadata(s3_object['Metadata'])