Skip to content

Commit e76bbd0

Browse files
[PRMP-1082] return different error body based on validation failure
1 parent 36942c8 commit e76bbd0

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

lambdas/handlers/post_document_review_handler.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from utils.decorators.ensure_env_var import ensure_environment_variables
1111
from utils.decorators.handle_lambda_exceptions import handle_lambda_exceptions
1212
from utils.decorators.set_audit_arg import set_request_context_for_logging
13-
from utils.exceptions import InvalidNhsNumberException, DocumentReviewException
13+
from utils.exceptions import InvalidNhsNumberException, DocumentReviewException, InvalidFileTypeException
1414
from utils.lambda_exceptions import DocumentReviewLambdaException
1515
from utils.lambda_response import ApiGatewayResponse
1616

@@ -55,8 +55,13 @@ def validate_event_body(body):
5555
event_body = DocumentReviewUploadEvent.model_validate_json(body)
5656

5757
return event_body
58-
except (ValidationError, InvalidNhsNumberException, DocumentReviewException) as e:
58+
except (ValidationError, InvalidNhsNumberException) as e:
5959
logger.error(e)
6060
raise DocumentReviewLambdaException(
6161
400, LambdaError.DocumentReviewUploadInvalidRequest
6262
)
63+
except InvalidFileTypeException as e:
64+
logger.error(e)
65+
raise DocumentReviewLambdaException(
66+
400, LambdaError.DocumentReviewUnsupportedFileType
67+
)

lambdas/models/document_review.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from enums.snomed_codes import SnomedCodes
99
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator, ValidationError
1010
from pydantic.alias_generators import to_camel, to_pascal
11-
from utils.exceptions import InvalidNhsNumberException, ConfigNotFoundException, DocumentReviewException
11+
from utils.exceptions import InvalidNhsNumberException, ConfigNotFoundException, InvalidFileTypeException
1212
from utils import upload_file_configs
1313
from utils.utilities import validate_nhs_number
1414

@@ -166,8 +166,8 @@ def validate_file_extension(self) -> Self:
166166

167167
for file in self.documents:
168168
if not is_file_type_allowed(file, accepted_file_types):
169-
raise DocumentReviewException("Invalid file extension.")
169+
raise InvalidFileTypeException("Invalid file extension.")
170170
return self
171171
except ConfigNotFoundException:
172-
raise DocumentReviewException("Unable to find file configuration.")
172+
raise InvalidFileTypeException("Unable to find file configuration.")
173173

lambdas/tests/unit/handlers/test_post_document_review_handler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,19 +221,19 @@ def test_validate_event_body_valid_event_returns_document_review_upload_event_mo
221221

222222

223223
def test_validate_event_body_throws_error_unsupported_snomed_code(invalid_event):
224-
invalid_event["body"] = INVALID_EVENT_UNSUPPORTED_SNOMED_CODE
224+
invalid_event["body"] = json.dumps(INVALID_EVENT_UNSUPPORTED_SNOMED_CODE)
225225
with pytest.raises(DocumentReviewLambdaException) as e:
226226
validate_event_body(invalid_event["body"])
227227
assert e.value.status_code == 400
228228
assert e.value.err_code == "UDR_4003"
229229

230230

231231
def test_validate_event_body_throws_error_unsupported_file_type(invalid_event):
232-
invalid_event["body"] = INVALID_EVENT_UNSUPPORTED_SNOMED_CODE
232+
invalid_event["body"] = json.dumps(INVALID_EVENT_INVALID_FILE_EXTENSION)
233233
with pytest.raises(DocumentReviewLambdaException) as e:
234234
validate_event_body(invalid_event["body"])
235235
assert e.value.status_code == 400
236-
assert e.value.err_code == "UDR_4003"
236+
assert e.value.err_code == "DRV_4006"
237237

238238

239239
def test_lambda_handler_calls_service_with_validated_event(

lambdas/utils/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,7 @@ class ReviewProcessCreateRecordException(Exception):
218218

219219
class CorruptedFileException(Exception):
220220
pass
221+
222+
223+
class InvalidFileTypeException(Exception):
224+
pass

0 commit comments

Comments
 (0)