|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import uuid |
| 4 | +from datetime import UTC, datetime |
| 5 | +from enum import Enum |
| 6 | +from http import HTTPStatus |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from fhir.resources.operationoutcome import OperationOutcome, OperationOutcomeIssue |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class FHIRIssueSeverity(str, Enum): |
| 15 | + FATAL = "fatal" |
| 16 | + ERROR = "error" |
| 17 | + WARNING = "warning" |
| 18 | + INFORMATION = "information" |
| 19 | + |
| 20 | + |
| 21 | +class FHIRIssueCode(str, Enum): |
| 22 | + FORBIDDEN = "forbidden" |
| 23 | + PROCESSING = "processing" |
| 24 | + VALUE = "value" |
| 25 | + |
| 26 | + |
| 27 | +class FHIRSpineErrorCode(str, Enum): |
| 28 | + INVALID_NHS_NUMBER = "INVALID_NHS_NUMBER" |
| 29 | + INVALID_PARAMETER = "INVALID_PARAMETER" |
| 30 | + INTERNAL_SERVER_ERROR = "INTERNAL_SERVER_ERROR" |
| 31 | + REFERENCE_NOT_FOUND = "REFERENCE_NOT_FOUND" |
| 32 | + |
| 33 | + |
| 34 | +class APIErrorResponse: |
| 35 | + def __init__( # noqa: PLR0913 |
| 36 | + self, |
| 37 | + status_code: HTTPStatus, |
| 38 | + fhir_issue_code: FHIRIssueCode, |
| 39 | + fhir_issue_severity: FHIRIssueSeverity, |
| 40 | + fhir_coding_system: str, |
| 41 | + fhir_error_code: str, |
| 42 | + fhir_display_message: str, |
| 43 | + ) -> None: |
| 44 | + self.status_code = status_code |
| 45 | + self.fhir_issue_code = fhir_issue_code |
| 46 | + self.fhir_issue_severity = fhir_issue_severity |
| 47 | + self.fhir_coding_system = fhir_coding_system |
| 48 | + self.fhir_error_code = fhir_error_code |
| 49 | + self.fhir_display_message = fhir_display_message |
| 50 | + |
| 51 | + def build_operation_outcome_issue(self, diagnostics: str, location: list[str] | None) -> OperationOutcomeIssue: |
| 52 | + details = { |
| 53 | + "coding": [ |
| 54 | + { |
| 55 | + "system": self.fhir_coding_system, |
| 56 | + "code": self.fhir_error_code, |
| 57 | + "display": self.fhir_display_message, |
| 58 | + } |
| 59 | + ] |
| 60 | + } |
| 61 | + return OperationOutcomeIssue( |
| 62 | + severity=self.fhir_issue_severity, |
| 63 | + code=self.fhir_issue_code, |
| 64 | + diagnostics=diagnostics, |
| 65 | + location=location, |
| 66 | + details=details, |
| 67 | + ) # pyright: ignore[reportCallIssue] |
| 68 | + |
| 69 | + def generate_response(self, diagnostics: str, location_param: str | None = None) -> dict[str, Any]: |
| 70 | + issue_location = [f"parameters/{location_param}"] if location_param else None |
| 71 | + |
| 72 | + problem = OperationOutcome( |
| 73 | + id=str(uuid.uuid4()), |
| 74 | + meta={"lastUpdated": datetime.now(UTC)}, |
| 75 | + issue=[self.build_operation_outcome_issue(diagnostics, issue_location)], |
| 76 | + ) # pyright: ignore[reportCallIssue] |
| 77 | + |
| 78 | + response_body = json.dumps(problem.model_dump(by_alias=True, mode="json")) |
| 79 | + |
| 80 | + return { |
| 81 | + "statusCode": self.status_code, |
| 82 | + "headers": {"Content-Type": "application/fhir+json"}, |
| 83 | + "body": response_body, |
| 84 | + } |
| 85 | + |
| 86 | + def log_and_generate_response( |
| 87 | + self, log_message: str, diagnostics: str, location_param: str | None = None |
| 88 | + ) -> dict[str, Any]: |
| 89 | + logger.error(log_message) |
| 90 | + return self.generate_response(diagnostics, location_param) |
| 91 | + |
| 92 | + |
| 93 | +INVALID_INCLUDE_ACTIONS_ERROR = APIErrorResponse( |
| 94 | + status_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 95 | + fhir_issue_code=FHIRIssueCode.VALUE, |
| 96 | + fhir_issue_severity=FHIRIssueSeverity.ERROR, |
| 97 | + fhir_coding_system="https://fhir.nhs.uk/STU3/ValueSet/Spine-ErrorOrWarningCode-1", |
| 98 | + fhir_error_code=FHIRSpineErrorCode.INVALID_PARAMETER, |
| 99 | + fhir_display_message="The supplied value was not recognised by the API.", |
| 100 | +) |
| 101 | + |
| 102 | +INVALID_CATEGORY_ERROR = APIErrorResponse( |
| 103 | + status_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 104 | + fhir_issue_code=FHIRIssueCode.VALUE, |
| 105 | + fhir_issue_severity=FHIRIssueSeverity.ERROR, |
| 106 | + fhir_coding_system="https://fhir.nhs.uk/STU3/ValueSet/Spine-ErrorOrWarningCode-1", |
| 107 | + fhir_error_code=FHIRSpineErrorCode.INVALID_PARAMETER, |
| 108 | + fhir_display_message="The supplied category was not recognised by the API.", |
| 109 | +) |
| 110 | + |
| 111 | +INVALID_CONDITION_FORMAT_ERROR = APIErrorResponse( |
| 112 | + status_code=HTTPStatus.BAD_REQUEST, |
| 113 | + fhir_issue_code=FHIRIssueCode.VALUE, |
| 114 | + fhir_issue_severity=FHIRIssueSeverity.ERROR, |
| 115 | + fhir_coding_system="https://fhir.nhs.uk/STU3/ValueSet/Spine-ErrorOrWarningCode-1", |
| 116 | + fhir_error_code=FHIRSpineErrorCode.INVALID_PARAMETER, |
| 117 | + fhir_display_message="The given conditions were not in the expected format.", |
| 118 | +) |
| 119 | + |
| 120 | +NHS_NUMBER_NOT_FOUND_ERROR = APIErrorResponse( |
| 121 | + status_code=HTTPStatus.NOT_FOUND, |
| 122 | + fhir_issue_code=FHIRIssueCode.PROCESSING, |
| 123 | + fhir_issue_severity=FHIRIssueSeverity.ERROR, |
| 124 | + fhir_coding_system="https://fhir.nhs.uk/STU3/ValueSet/Spine-ErrorOrWarningCode-1", |
| 125 | + fhir_error_code=FHIRSpineErrorCode.REFERENCE_NOT_FOUND, |
| 126 | + fhir_display_message="The given NHS number was not found in our datasets. " |
| 127 | + "This could be because the number is incorrect or " |
| 128 | + "some other reason we cannot process that number.", |
| 129 | +) |
| 130 | + |
| 131 | +INTERNAL_SERVER_ERROR = APIErrorResponse( |
| 132 | + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, |
| 133 | + fhir_issue_code=FHIRIssueCode.PROCESSING, |
| 134 | + fhir_issue_severity=FHIRIssueSeverity.ERROR, |
| 135 | + fhir_coding_system="https://fhir.nhs.uk/STU3/ValueSet/Spine-ErrorOrWarningCode-1", |
| 136 | + fhir_error_code=FHIRSpineErrorCode.INTERNAL_SERVER_ERROR, |
| 137 | + fhir_display_message="An unexpected internal server error occurred.", |
| 138 | +) |
| 139 | + |
| 140 | +NHS_NUMBER_MISMATCH_ERROR = APIErrorResponse( |
| 141 | + status_code=HTTPStatus.FORBIDDEN, |
| 142 | + fhir_issue_code=FHIRIssueCode.FORBIDDEN, |
| 143 | + fhir_issue_severity=FHIRIssueSeverity.ERROR, |
| 144 | + fhir_coding_system="https://fhir.nhs.uk/STU3/ValueSet/Spine-ErrorOrWarningCode-1", |
| 145 | + fhir_error_code=FHIRSpineErrorCode.INVALID_NHS_NUMBER, |
| 146 | + fhir_display_message="The provided NHS number does not match the record.", |
| 147 | +) |
0 commit comments