From 245407836b99dd103d85fe3b0736ecf8dd78e416 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 18 Dec 2024 14:28:10 -0400 Subject: [PATCH] Fix/handle api exception bulk send (#2022) * fix(start_job): add exception handling in case API throws an annual limits error at this point; update the expected error message strings * fix(check.html): don't render the template preview in the case of an API exception * chore: add tests * fix: pass time_to_reset into the view --- app/main/views/send.py | 16 +++- app/templates/views/notifications/check.html | 4 +- tests/app/main/views/test_send.py | 98 ++++++++++++++++++++ 3 files changed, 113 insertions(+), 5 deletions(-) diff --git a/app/main/views/send.py b/app/main/views/send.py index 53acc9ba6..f0e4e8640 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -888,8 +888,15 @@ def check_notification_preview(service_id, template_id, filetype): @main.route("/services//start-job/", methods=["POST"]) @user_has_permissions("send_messages", restrict_admin_usage=True) def start_job(service_id, upload_id): - job_api_client.create_job(upload_id, service_id, scheduled_for=request.form.get("scheduled_for", "")) - + try: + job_api_client.create_job(upload_id, service_id, scheduled_for=request.form.get("scheduled_for", "")) + except HTTPError as exception: + return render_template( + "views/notifications/check.html", + time_to_reset=get_limit_reset_time_et(), + **(get_template_error_dict(exception) if exception else {}), + template=None, + ) session.pop("sender_id", None) return redirect( @@ -1107,11 +1114,12 @@ def get_template_error_dict(exception): error = "too-many-sms-messages" elif "Content for template has a character count greater than the limit of" in exception.message: error = "message-too-long" - elif "Exceeded annual email sending limit" in exception.message: + elif "Exceeded annual email sending" in exception.message: error = "too-many-email-annual" - elif "Exceeded annual SMS sending limit" in exception.message: + elif "Exceeded annual SMS sending" in exception.message: error = "too-many-sms-annual" else: + current_app.logger.error("Unhandled exception from API: {}".format(exception)) raise exception return { diff --git a/app/templates/views/notifications/check.html b/app/templates/views/notifications/check.html index 0161a74f1..e9d61b7f9 100644 --- a/app/templates/views/notifications/check.html +++ b/app/templates/views/notifications/check.html @@ -88,7 +88,9 @@

{{_('You cannot send this email message today') }}