Catch cancel_letter errors from API

When we catch such error, if the message is recognised,
show the message and redirect user to view_notification page.
This commit is contained in:
Pea Tyczynska
2021-07-27 15:05:45 +01:00
parent 8d9e415539
commit e1420e7ff7
2 changed files with 50 additions and 4 deletions

View File

@@ -17,7 +17,7 @@ from flask import (
stream_with_context,
url_for,
)
from notifications_python_client.errors import APIError
from notifications_python_client.errors import APIError, HTTPError
from notifications_utils.letter_timings import (
get_letter_timings,
letter_can_be_cancelled,
@@ -185,7 +185,14 @@ def view_notification(service_id, notification_id):
def cancel_letter(service_id, notification_id):
if request.method == 'POST':
notification_api_client.update_notification_to_cancelled(current_service.id, notification_id)
try:
notification_api_client.update_notification_to_cancelled(current_service.id, notification_id)
except HTTPError as e:
message_fragments = ["already been cancelled", "too late to cancel"]
if e.status_code == 400 and any(fragment in e.message for fragment in message_fragments):
flash(e.message)
else:
raise e
return redirect(url_for('main.view_notification', service_id=service_id, notification_id=notification_id))
flash("Are you sure you want to cancel sending this letter?", 'cancel')