mirror of
https://github.com/GSA/notifications-api.git
synced 2026-05-05 16:48:31 -04:00
karlify the exception messages
also create a PDFNotReadyError class, separate to BadRequestError, to imply to the end user that this is something they should handle separately to all the other errors
This commit is contained in:
@@ -57,6 +57,11 @@ class BadRequestError(InvalidRequest):
|
|||||||
self.message = message if message else self.message
|
self.message = message if message else self.message
|
||||||
|
|
||||||
|
|
||||||
|
class PDFNotReadyError(BadRequestError):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(message='PDF not available yet, try again later', status_code=400)
|
||||||
|
|
||||||
|
|
||||||
def register_errors(blueprint):
|
def register_errors(blueprint):
|
||||||
@blueprint.errorhandler(InvalidEmailError)
|
@blueprint.errorhandler(InvalidEmailError)
|
||||||
def invalid_format(error):
|
def invalid_format(error):
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from app import api_user, authenticated_service
|
|||||||
from app.dao import notifications_dao
|
from app.dao import notifications_dao
|
||||||
from app.letters.utils import get_letter_pdf
|
from app.letters.utils import get_letter_pdf
|
||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
from app.v2.errors import BadRequestError
|
from app.v2.errors import BadRequestError, PDFNotReadyError
|
||||||
from app.v2.notifications import v2_notification_blueprint
|
from app.v2.notifications import v2_notification_blueprint
|
||||||
from app.v2.notifications.notification_schemas import get_notifications_request, notification_by_id
|
from app.v2.notifications.notification_schemas import get_notifications_request, notification_by_id
|
||||||
from app.models import (
|
from app.models import (
|
||||||
@@ -36,29 +36,21 @@ def get_pdf_for_notification(notification_id):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if notification.notification_type != LETTER_TYPE:
|
if notification.notification_type != LETTER_TYPE:
|
||||||
raise BadRequestError(message="Notification is not a letter", status_code=400)
|
raise BadRequestError(message="Notification is not a letter")
|
||||||
|
|
||||||
if notification.status in {
|
if notification.status == NOTIFICATION_VIRUS_SCAN_FAILED:
|
||||||
NOTIFICATION_PENDING_VIRUS_CHECK,
|
raise BadRequestError(message='Document did not pass the virus scan')
|
||||||
NOTIFICATION_VIRUS_SCAN_FAILED,
|
|
||||||
NOTIFICATION_TECHNICAL_FAILURE,
|
if notification.status == NOTIFICATION_TECHNICAL_FAILURE:
|
||||||
}:
|
raise BadRequestError(message='PDF not available for letters in status {}'.format(notification.status))
|
||||||
raise BadRequestError(
|
|
||||||
message='PDF not available for letters in status {}'.format(notification.status),
|
if notification.status == NOTIFICATION_PENDING_VIRUS_CHECK:
|
||||||
status_code=400
|
raise PDFNotReadyError()
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pdf_data = get_letter_pdf(notification)
|
pdf_data = get_letter_pdf(notification)
|
||||||
except Exception:
|
except Exception:
|
||||||
# this probably means it's a templated letter that hasn't been created yet
|
raise PDFNotReadyError()
|
||||||
current_app.logger.info('PDF not found for notification id {} status {}'.format(
|
|
||||||
notification.id,
|
|
||||||
notification.status
|
|
||||||
))
|
|
||||||
raise BadRequestError(
|
|
||||||
message='PDF not available for letter, try again later',
|
|
||||||
status_code=400)
|
|
||||||
|
|
||||||
return send_file(filename_or_fp=BytesIO(pdf_data), mimetype='application/pdf')
|
return send_file(filename_or_fp=BytesIO(pdf_data), mimetype='application/pdf')
|
||||||
|
|
||||||
|
|||||||
@@ -690,22 +690,22 @@ def test_get_pdf_for_notification_returns_400_if_pdf_not_found(
|
|||||||
|
|
||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
assert response.json['errors'] == [{
|
assert response.json['errors'] == [{
|
||||||
'error': 'BadRequestError',
|
'error': 'PDFNotReadyError',
|
||||||
'message': 'PDF not available for letter, try again later'
|
'message': 'PDF not available yet, try again later'
|
||||||
}]
|
}]
|
||||||
mock_get_letter_pdf.assert_called_once_with(sample_letter_notification)
|
mock_get_letter_pdf.assert_called_once_with(sample_letter_notification)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('status', [
|
@pytest.mark.parametrize('status, expected_message', [
|
||||||
'pending-virus-check',
|
('virus-scan-failed', 'Document did not pass the virus scan'),
|
||||||
'virus-scan-failed',
|
('technical-failure', 'PDF not available for letters in status technical-failure'),
|
||||||
'technical-failure',
|
|
||||||
])
|
])
|
||||||
def test_get_pdf_for_notification_only_returns_pdf_content_if_right_status(
|
def test_get_pdf_for_notification_only_returns_pdf_content_if_right_status(
|
||||||
client,
|
client,
|
||||||
sample_letter_notification,
|
sample_letter_notification,
|
||||||
mocker,
|
mocker,
|
||||||
status,
|
status,
|
||||||
|
expected_message
|
||||||
):
|
):
|
||||||
mock_get_letter_pdf = mocker.patch('app.v2.notifications.get_notifications.get_letter_pdf', return_value=b'foo')
|
mock_get_letter_pdf = mocker.patch('app.v2.notifications.get_notifications.get_letter_pdf', return_value=b'foo')
|
||||||
sample_letter_notification.status = status
|
sample_letter_notification.status = status
|
||||||
@@ -719,7 +719,7 @@ def test_get_pdf_for_notification_only_returns_pdf_content_if_right_status(
|
|||||||
assert response.status_code == 400
|
assert response.status_code == 400
|
||||||
assert response.json['errors'] == [{
|
assert response.json['errors'] == [{
|
||||||
'error': 'BadRequestError',
|
'error': 'BadRequestError',
|
||||||
'message': 'PDF not available for letters in status {}'.format(status)
|
'message': expected_message
|
||||||
}]
|
}]
|
||||||
assert mock_get_letter_pdf.called is False
|
assert mock_get_letter_pdf.called is False
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user