From 0c8dd247f9cd4c704c871cf00e5bb244bda436cb Mon Sep 17 00:00:00 2001 From: Pea Tyczynska Date: Fri, 23 Jul 2021 17:26:01 +0100 Subject: [PATCH] Show separate error for when user tries to cancel letter that is already cancelled vs when it is too late to cancel letter vs when we don't know what's the cause of failure. This is so we could show useful error messages to the users and also for better debugging. --- app/service/rest.py | 17 ++++++++++++++--- tests/app/service/test_rest.py | 9 ++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/service/rest.py b/app/service/rest.py index 86908a92b..a22373b02 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -2,7 +2,10 @@ import itertools from datetime import datetime from flask import Blueprint, current_app, jsonify, request -from notifications_utils.letter_timings import letter_can_be_cancelled +from notifications_utils.letter_timings import ( + letter_can_be_cancelled, + too_late_to_cancel_letter, +) from notifications_utils.timezones import convert_utc_to_bst from sqlalchemy.exc import IntegrityError from sqlalchemy.orm.exc import NoResultFound @@ -473,9 +476,17 @@ def cancel_notification_for_service(service_id, notification_id): raise InvalidRequest('Notification cannot be cancelled - only letters can be cancelled', status_code=400) elif not letter_can_be_cancelled(notification.status, notification.created_at): print_day = letter_print_day(notification.created_at) - + if too_late_to_cancel_letter(notification.created_at): + message = "It’s too late to cancel this letter. Printing started {} at 5.30pm".format(print_day) + elif notification.status == 'cancelled': + message = "This letter has already been cancelled." + else: + message = ( + f"We could not cancel this letter. " + f"Letter status: {notification.status}, created_at: {notification.created_at}" + ) raise InvalidRequest( - "It’s too late to cancel this letter. Printing started {} at 5.30pm".format(print_day), + message, status_code=400) updated_notification = notifications_dao.update_notification_status_by_id( diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index f80063dd6..b16ab5fb8 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -3445,6 +3445,7 @@ def test_cancel_notification_for_service_raises_invalid_request_when_letter_is_i notification_status, ): sample_letter_notification.status = notification_status + sample_letter_notification.created_at = datetime.now() response = admin_request.post( 'service.cancel_notification_for_service', @@ -3452,7 +3453,13 @@ def test_cancel_notification_for_service_raises_invalid_request_when_letter_is_i notification_id=sample_letter_notification.id, _expected_status=400 ) - assert response['message'] == 'It’s too late to cancel this letter. Printing started today at 5.30pm' + if notification_status == 'cancelled': + assert response['message'] == 'This letter has already been cancelled.' + else: + assert response['message'] == ( + f"We could not cancel this letter. " + f"Letter status: {notification_status}, created_at: 2018-07-07 12:00:00" + ) assert response['result'] == 'error'