mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-20 14:29:25 -04:00
Merge pull request #2696 from alphagov/doc-dl-errors
handle document download errors properly
This commit is contained in:
@@ -12,13 +12,8 @@ class DocumentDownloadError(Exception):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_exception(cls, e):
|
def from_exception(cls, e):
|
||||||
try:
|
message = e.response.json()['error']
|
||||||
message = e.response.json()['error']
|
status_code = e.response.status_code
|
||||||
status_code = e.response.status_code
|
|
||||||
except (TypeError, ValueError, AttributeError, KeyError):
|
|
||||||
message = 'connection error'
|
|
||||||
status_code = 503
|
|
||||||
|
|
||||||
return cls(message, status_code)
|
return cls(message, status_code)
|
||||||
|
|
||||||
|
|
||||||
@@ -45,11 +40,16 @@ class DocumentDownloadClient:
|
|||||||
|
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
error = DocumentDownloadError.from_exception(e)
|
# if doc dl responds with a non-400, (eg 403) it's referring to credentials that the API and Doc DL use.
|
||||||
current_app.logger.warning(
|
# we don't want to tell users about that, so anything that isn't a 400 (virus scan failed or file type
|
||||||
'Document download request failed with error: {}'.format(error.message)
|
# unrecognised) should be raised as a 500 internal server error here.
|
||||||
)
|
if e.response.status_code == 400:
|
||||||
|
error = DocumentDownloadError.from_exception(e)
|
||||||
raise error
|
current_app.logger.info(
|
||||||
|
'Document download request failed with error: {}'.format(error.message)
|
||||||
|
)
|
||||||
|
raise error
|
||||||
|
else:
|
||||||
|
raise Exception(f'Unhandled document download error: {e.response.text}')
|
||||||
|
|
||||||
return response.json()['document']['url']
|
return response.json()['document']['url']
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ def get_pdf_for_notification(notification_id):
|
|||||||
raise BadRequestError(message="Notification is not a letter")
|
raise BadRequestError(message="Notification is not a letter")
|
||||||
|
|
||||||
if notification.status == NOTIFICATION_VIRUS_SCAN_FAILED:
|
if notification.status == NOTIFICATION_VIRUS_SCAN_FAILED:
|
||||||
raise BadRequestError(message='Document did not pass the virus scan')
|
raise BadRequestError(message='File did not pass the virus scan')
|
||||||
|
|
||||||
if notification.status == 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))
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import requests
|
|
||||||
import requests_mock
|
import requests_mock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -33,26 +32,27 @@ def test_upload_document(document_download):
|
|||||||
assert resp == 'https://document-download/services/service-id/documents/uploaded-url'
|
assert resp == 'https://document-download/services/service-id/documents/uploaded-url'
|
||||||
|
|
||||||
|
|
||||||
def test_should_raise_for_status(document_download):
|
def test_should_raise_400s_as_DocumentDownloadErrors(document_download):
|
||||||
with pytest.raises(DocumentDownloadError) as excinfo, requests_mock.Mocker() as request_mock:
|
with pytest.raises(DocumentDownloadError) as excinfo, requests_mock.Mocker() as request_mock:
|
||||||
request_mock.post('https://document-download/services/service-id/documents', json={
|
request_mock.post('https://document-download/services/service-id/documents', json={
|
||||||
'error': 'Invalid encoding'
|
'error': 'Invalid mime type'
|
||||||
}, status_code=403)
|
}, status_code=400)
|
||||||
|
|
||||||
document_download.upload_document('service-id', 'abababab')
|
document_download.upload_document('service-id', 'abababab')
|
||||||
|
|
||||||
assert excinfo.value.message == 'Invalid encoding'
|
assert excinfo.value.message == 'Invalid mime type'
|
||||||
assert excinfo.value.status_code == 403
|
assert excinfo.value.status_code == 400
|
||||||
|
|
||||||
|
|
||||||
def test_should_raise_for_connection_errors(document_download):
|
def test_should_raise_non_400_statuses_as_exceptions(document_download):
|
||||||
with pytest.raises(DocumentDownloadError) as excinfo, requests_mock.Mocker() as request_mock:
|
with pytest.raises(Exception) as excinfo, requests_mock.Mocker() as request_mock:
|
||||||
request_mock.post(
|
request_mock.post(
|
||||||
'https://document-download/services/service-id/documents',
|
'https://document-download/services/service-id/documents',
|
||||||
exc=requests.exceptions.ConnectTimeout
|
json={'error': 'Auth Error Of Some Kind'},
|
||||||
|
status_code=403
|
||||||
)
|
)
|
||||||
|
|
||||||
document_download.upload_document('service-id', 'abababab')
|
document_download.upload_document('service-id', 'abababab')
|
||||||
|
|
||||||
assert excinfo.value.message == 'connection error'
|
assert type(excinfo.value) == Exception # make sure it's a base exception, so will be handled as a 500 by v2 api
|
||||||
assert excinfo.value.status_code == 503
|
assert str(excinfo.value) == 'Unhandled document download error: {"error": "Auth Error Of Some Kind"}'
|
||||||
|
|||||||
@@ -250,7 +250,7 @@ def create_notification(
|
|||||||
updated_at = updated_at or datetime.utcnow()
|
updated_at = updated_at or datetime.utcnow()
|
||||||
|
|
||||||
if not one_off and (job is None and api_key is None):
|
if not one_off and (job is None and api_key is None):
|
||||||
# we didn't specify in test - lets create it
|
# we did not specify in test - lets create it
|
||||||
api_key = ApiKey.query.filter(ApiKey.service == template.service, ApiKey.key_type == key_type).first()
|
api_key = ApiKey.query.filter(ApiKey.service == template.service, ApiKey.key_type == key_type).first()
|
||||||
if not api_key:
|
if not api_key:
|
||||||
api_key = create_api_key(template.service, key_type=key_type)
|
api_key = create_api_key(template.service, key_type=key_type)
|
||||||
|
|||||||
@@ -703,7 +703,7 @@ def test_get_pdf_for_notification_returns_400_if_pdf_not_found(
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('status, expected_message', [
|
@pytest.mark.parametrize('status, expected_message', [
|
||||||
('virus-scan-failed', 'Document did not pass the virus scan'),
|
('virus-scan-failed', 'File did not pass the virus scan'),
|
||||||
('technical-failure', 'PDF not available for letters in status technical-failure'),
|
('technical-failure', 'PDF not available for letters in status 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(
|
||||||
|
|||||||
Reference in New Issue
Block a user