Skip to content

Commit f1615c8

Browse files
committed
errors
1 parent 4b67947 commit f1615c8

26 files changed

+157
-332
lines changed

lambdas/backend/src/controller/aws_apig_event_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
from aws_lambda_typing.events import APIGatewayProxyEventV1
66

7-
from common.models.api_errors import UnauthorizedError
8-
from common.models.errors import ResourceVersionNotProvided
97
from controller.constants import E_TAG_HEADER_NAME, SUPPLIER_SYSTEM_HEADER_NAME
8+
from models.errors import ResourceVersionNotProvided, UnauthorizedError
109
from utils import dict_utils
1110

1211

lambdas/backend/src/controller/fhir_api_exception_handler.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55
from typing import Callable, Type
66

77
from common.clients import logger
8-
from common.models.api_errors import (
9-
Code,
8+
from common.models.constants import GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE
9+
from common.models.errors import (
10+
CustomValidationError,
11+
IdentifierDuplicationError,
1012
InconsistentIdentifierError,
11-
InconsistentIdError,
1213
InconsistentResourceVersion,
14+
ResourceNotFoundError,
15+
)
16+
from controller.aws_apig_response_utils import create_response
17+
from models.errors import (
18+
Code,
19+
InconsistentIdError,
1320
InvalidImmunizationId,
1421
InvalidJsonError,
1522
InvalidResourceVersion,
23+
ResourceVersionNotProvided,
1624
Severity,
1725
UnauthorizedError,
1826
UnauthorizedVaxError,
1927
UnhandledResponseError,
2028
create_operation_outcome,
2129
)
22-
from common.models.constants import GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE
23-
from common.models.errors import (
24-
CustomValidationError,
25-
IdentifierDuplicationError,
26-
ResourceNotFoundError,
27-
ResourceVersionNotProvided,
28-
)
29-
from controller.aws_apig_response_utils import create_response
3030

3131
_CUSTOM_EXCEPTION_TO_STATUS_MAP: dict[Type[Exception], int] = {
3232
InconsistentResourceVersion: 400,

lambdas/backend/src/controller/fhir_controller.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,27 @@
1010

1111
from aws_lambda_typing.events import APIGatewayProxyEventV1
1212

13-
from common.models.api_errors import (
13+
from common.models.utils.generic_utils import check_keys_in_sources
14+
from controller.aws_apig_event_utils import (
15+
get_path_parameter,
16+
get_resource_version_header,
17+
get_supplier_system_header,
18+
)
19+
from controller.aws_apig_response_utils import create_response
20+
from controller.constants import E_TAG_HEADER_NAME
21+
from controller.fhir_api_exception_handler import fhir_api_exception_handler
22+
from models.errors import (
1423
Code,
1524
InconsistentIdError,
1625
InvalidImmunizationId,
1726
InvalidJsonError,
1827
InvalidResourceVersion,
28+
ParameterException,
1929
Severity,
2030
UnauthorizedError,
2131
UnauthorizedVaxError,
2232
create_operation_outcome,
2333
)
24-
from common.models.errors import (
25-
ParameterException,
26-
)
27-
from common.models.utils.generic_utils import check_keys_in_sources
28-
from controller.aws_apig_event_utils import (
29-
get_path_parameter,
30-
get_resource_version_header,
31-
get_supplier_system_header,
32-
)
33-
from controller.aws_apig_response_utils import create_response
34-
from controller.constants import E_TAG_HEADER_NAME
35-
from controller.fhir_api_exception_handler import fhir_api_exception_handler
3634
from parameter_parser import create_query_string, process_params, process_search_params
3735
from repository.fhir_repository import ImmunizationRepository, create_table
3836
from service.fhir_service import FhirService, get_service_url

lambdas/shared/src/common/models/api_errors.py renamed to lambdas/backend/src/models/errors.py

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -66,51 +66,41 @@ def to_operation_outcome() -> dict:
6666

6767

6868
@dataclass
69-
class UnauthorizedVaxOnRecordError(RuntimeError):
70-
@staticmethod
71-
def to_operation_outcome() -> dict:
72-
msg = "Unauthorized request for vaccine type present in the stored immunization resource"
73-
return create_operation_outcome(
74-
resource_id=str(uuid.uuid4()),
75-
severity=Severity.error,
76-
code=Code.forbidden,
77-
diagnostics=msg,
78-
)
79-
80-
81-
class MandatoryError(Exception):
82-
def __init__(self, message=None):
83-
self.message = message
69+
class ResourceVersionNotProvided(RuntimeError):
70+
"""Return this error when client has failed to provide the FHIR resource version where required"""
8471

72+
resource_type: str
8573

86-
@dataclass
87-
class InvalidImmunizationId(ApiValidationError):
88-
"""Use this when the unique Immunization ID is invalid"""
74+
def __str__(self):
75+
return f"Validation errors: {self.resource_type} resource version not specified in the request headers"
8976

9077
def to_operation_outcome(self) -> dict:
9178
return create_operation_outcome(
9279
resource_id=str(uuid.uuid4()),
9380
severity=Severity.error,
94-
code=Code.invalid,
95-
diagnostics="Validation errors: the provided event ID is either missing or not in the expected format.",
81+
code=Code.invariant,
82+
diagnostics=self.__str__(),
9683
)
9784

9885

9986
@dataclass
100-
class InvalidPatientId(ApiValidationError):
101-
"""Use this when NHS Number is invalid or doesn't exist"""
102-
103-
patient_identifier: str
87+
class ParameterException(RuntimeError):
88+
message: str
10489

10590
def __str__(self):
106-
return f"NHS Number: {self.patient_identifier} is invalid or it doesn't exist."
91+
return self.message
92+
93+
94+
@dataclass
95+
class InvalidImmunizationId(ApiValidationError):
96+
"""Use this when the unique Immunization ID is invalid"""
10797

10898
def to_operation_outcome(self) -> dict:
10999
return create_operation_outcome(
110100
resource_id=str(uuid.uuid4()),
111101
severity=Severity.error,
112-
code=Code.server_error,
113-
diagnostics=self.__str__(),
102+
code=Code.invalid,
103+
diagnostics="Validation errors: the provided event ID is either missing or not in the expected format.",
114104
)
115105

116106

@@ -130,18 +120,6 @@ def to_operation_outcome(self) -> dict:
130120
)
131121

132122

133-
@dataclass
134-
class InconsistentIdentifierError(ApiValidationError):
135-
"""Use this when the local identifier in the payload does not match the existing identifier for the update."""
136-
137-
msg: str
138-
139-
def to_operation_outcome(self) -> dict:
140-
return create_operation_outcome(
141-
resource_id=str(uuid.uuid4()), severity=Severity.error, code=Code.invariant, diagnostics=self.msg
142-
)
143-
144-
145123
@dataclass
146124
class InconsistentIdError(ApiValidationError):
147125
"""Use this when the specified id in the message is inconsistent with the path
@@ -164,21 +142,6 @@ def to_operation_outcome(self) -> dict:
164142
)
165143

166144

167-
@dataclass
168-
class InconsistentResourceVersion(ApiValidationError):
169-
"""Use this when the resource version in the request and actual resource version do not match"""
170-
171-
message: str
172-
173-
def to_operation_outcome(self) -> dict:
174-
return create_operation_outcome(
175-
resource_id=str(uuid.uuid4()),
176-
severity=Severity.error,
177-
code=Code.invariant,
178-
diagnostics=self.message,
179-
)
180-
181-
182145
@dataclass
183146
class InvalidJsonError(RuntimeError):
184147
"""Raised when client provides an invalid JSON payload"""

lambdas/backend/src/parameter_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
from aws_lambda_typing.events import APIGatewayProxyEventV1
88

99
from common.models.constants import Constants
10-
from common.models.errors import ParameterException
1110
from common.models.utils.generic_utils import nhs_number_mod11_check
1211
from common.redis_client import get_redis_client
12+
from models.errors import ParameterException
1313

1414
ERROR_MESSAGE_DUPLICATED_PARAMETERS = 'Parameters may not be duplicated. Use commas for "or".'
1515

lambdas/backend/src/repository/fhir_repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
from mypy_boto3_dynamodb.service_resource import DynamoDBServiceResource, Table
1414
from responses import logger
1515

16-
from common.models.api_errors import UnhandledResponseError
1716
from common.models.constants import Constants
1817
from common.models.errors import ResourceNotFoundError
1918
from common.models.immunization_record_metadata import ImmunizationRecordMetadata
2019
from common.models.utils.generic_utils import get_contained_patient
2120
from common.models.utils.validation_utils import (
2221
get_vaccine_type,
2322
)
23+
from models.errors import UnhandledResponseError
2424

2525

2626
def create_table(table_name=None, endpoint_url=None, region_name="eu-west-2"):

lambdas/backend/src/search_imms_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
from aws_lambda_typing import context as context_
1010
from aws_lambda_typing import events
1111

12-
from common.models.api_errors import Code, Severity, create_operation_outcome
1312
from common.models.constants import GENERIC_SERVER_ERROR_DIAGNOSTICS_MESSAGE, MAX_RESPONSE_SIZE_BYTES
1413
from controller.aws_apig_response_utils import create_response
1514
from controller.fhir_controller import FhirController, make_controller
1615
from log_structure import function_info
16+
from models.errors import Code, Severity, create_operation_outcome
1717

1818
logging.basicConfig(level="INFO")
1919
logger = logging.getLogger()

lambdas/backend/src/service/fhir_service.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020
import parameter_parser
2121
from authorisation.api_operation_code import ApiOperationCode
2222
from authorisation.authoriser import Authoriser
23-
from common.models.api_errors import (
24-
MandatoryError,
25-
UnauthorizedVaxError,
26-
)
2723
from common.models.errors import (
2824
CustomValidationError,
2925
IdentifierDuplicationError,
26+
MandatoryError,
3027
ResourceNotFoundError,
3128
)
3229
from common.models.fhir_immunization import ImmunizationValidator
@@ -41,6 +38,7 @@
4138
validate_resource_versions_match,
4239
)
4340
from filter import Filter
41+
from models.errors import UnauthorizedVaxError
4442
from repository.fhir_repository import ImmunizationRepository
4543

4644
logging.basicConfig(level="INFO")

lambdas/backend/tests/controller/test_fhir_api_exception_handler.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
import unittest
33
from unittest.mock import patch
44

5-
from common.models.api_errors import (
5+
from common.models.errors import (
6+
CustomValidationError,
7+
IdentifierDuplicationError,
68
InconsistentIdentifierError,
7-
InconsistentIdError,
89
InconsistentResourceVersion,
10+
ResourceNotFoundError,
11+
)
12+
from controller.fhir_api_exception_handler import fhir_api_exception_handler
13+
from models.errors import (
14+
InconsistentIdError,
915
InvalidJsonError,
1016
InvalidResourceVersion,
17+
ResourceVersionNotProvided,
1118
UnauthorizedError,
1219
UnauthorizedVaxError,
1320
UnhandledResponseError,
1421
)
15-
from common.models.errors import (
16-
CustomValidationError,
17-
IdentifierDuplicationError,
18-
ResourceNotFoundError,
19-
ResourceVersionNotProvided,
20-
)
21-
from controller.fhir_api_exception_handler import fhir_api_exception_handler
2222

2323

2424
class TestFhirApiExceptionHandler(unittest.TestCase):

lambdas/backend/tests/controller/test_fhir_controller.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
from fhir.resources.R4B.bundle import Bundle
1111
from fhir.resources.R4B.immunization import Immunization
1212

13-
from common.models.api_errors import (
14-
UnauthorizedVaxError,
15-
UnhandledResponseError,
16-
)
1713
from common.models.errors import (
1814
CustomValidationError,
19-
ParameterException,
2015
ResourceNotFoundError,
2116
)
2217
from controller.aws_apig_response_utils import create_response
2318
from controller.fhir_controller import FhirController
19+
from models.errors import (
20+
ParameterException,
21+
UnauthorizedVaxError,
22+
UnhandledResponseError,
23+
)
2424
from parameter_parser import patient_identifier_system, process_search_params
2525
from repository.fhir_repository import ImmunizationRepository
2626
from service.fhir_service import FhirService

0 commit comments

Comments
 (0)