|
| 1 | +import uuid |
| 2 | +from dataclasses import dataclass |
| 3 | +from enum import Enum |
| 4 | + |
| 5 | + |
| 6 | +class Severity(str, Enum): |
| 7 | + error = "error" |
| 8 | + warning = "warning" |
| 9 | + |
| 10 | + |
| 11 | +class Code(str, Enum): |
| 12 | + forbidden = "forbidden" |
| 13 | + not_found = "not-found" |
| 14 | + invalid = "invalid or missing access token" |
| 15 | + server_error = "internal server error" |
| 16 | + invariant = "invariant" |
| 17 | + incomplete = "parameter-incomplete" |
| 18 | + duplicate = "duplicate" |
| 19 | + # Added an unauthorized code its used when returning a response for an unauthorized vaccine type search. |
| 20 | + unauthorized = "unauthorized" |
| 21 | + |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class UnauthorizedError(RuntimeError): |
| 25 | + response: dict | str |
| 26 | + message: str |
| 27 | + |
| 28 | + def __str__(self): |
| 29 | + return f"{self.message}\n{self.response}" |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def to_operation_outcome() -> dict: |
| 33 | + msg = "Unauthorized request" |
| 34 | + return create_operation_outcome( |
| 35 | + resource_id=str(uuid.uuid4()), |
| 36 | + severity=Severity.error, |
| 37 | + code=Code.forbidden, |
| 38 | + diagnostics=msg, |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +@dataclass |
| 43 | +class TokenValidationError(RuntimeError): |
| 44 | + response: dict | str |
| 45 | + message: str |
| 46 | + |
| 47 | + def __str__(self): |
| 48 | + return f"{self.message}\n{self.response}" |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def to_operation_outcome() -> dict: |
| 52 | + msg = "Missing/Invalid Token" |
| 53 | + return create_operation_outcome( |
| 54 | + resource_id=str(uuid.uuid4()), |
| 55 | + severity=Severity.error, |
| 56 | + code=Code.invalid, |
| 57 | + diagnostics=msg, |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +@dataclass |
| 62 | +class ConflictError(RuntimeError): |
| 63 | + response: dict | str |
| 64 | + message: str |
| 65 | + |
| 66 | + def __str__(self): |
| 67 | + return f"{self.message}\n{self.response}" |
| 68 | + |
| 69 | + @staticmethod |
| 70 | + def to_operation_outcome() -> dict: |
| 71 | + msg = "Conflict" |
| 72 | + return create_operation_outcome( |
| 73 | + resource_id=str(uuid.uuid4()), |
| 74 | + severity=Severity.error, |
| 75 | + code=Code.duplicate, |
| 76 | + diagnostics=msg, |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +@dataclass |
| 81 | +class ResourceNotFoundError(RuntimeError): |
| 82 | + """Return this error when the requested resource does not exist or not complete""" |
| 83 | + |
| 84 | + response: None |
| 85 | + message: str |
| 86 | + |
| 87 | + def __str__(self): |
| 88 | + return f"{self.message}\n{self.response}" |
| 89 | + |
| 90 | + def to_operation_outcome(self) -> dict: |
| 91 | + return create_operation_outcome( |
| 92 | + resource_id=str(uuid.uuid4()), |
| 93 | + severity=Severity.error, |
| 94 | + code=Code.not_found, |
| 95 | + diagnostics=self.__str__(), |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +@dataclass |
| 100 | +class UnhandledResponseError(RuntimeError): |
| 101 | + """Use this unhandled errors""" |
| 102 | + |
| 103 | + response: dict | str |
| 104 | + message: str |
| 105 | + |
| 106 | + def __str__(self): |
| 107 | + return f"{self.message}\n{self.response}" |
| 108 | + |
| 109 | + def to_operation_outcome(self) -> dict: |
| 110 | + return create_operation_outcome( |
| 111 | + resource_id=str(uuid.uuid4()), |
| 112 | + severity=Severity.error, |
| 113 | + code=Code.server_error, |
| 114 | + diagnostics=self.__str__(), |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +@dataclass |
| 119 | +class BadRequestError(RuntimeError): |
| 120 | + """Use when payload is missing required parameters""" |
| 121 | + |
| 122 | + response: dict | str |
| 123 | + message: str |
| 124 | + |
| 125 | + def __str__(self): |
| 126 | + return f"{self.message}\n{self.response}" |
| 127 | + |
| 128 | + def to_operation_outcome(self) -> dict: |
| 129 | + return create_operation_outcome( |
| 130 | + resource_id=str(uuid.uuid4()), |
| 131 | + severity=Severity.error, |
| 132 | + code=Code.incomplete, |
| 133 | + diagnostics=self.__str__(), |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +@dataclass |
| 138 | +class ServerError(RuntimeError): |
| 139 | + """Use when there is a server error""" |
| 140 | + |
| 141 | + response: dict | str |
| 142 | + message: str |
| 143 | + |
| 144 | + def __str__(self): |
| 145 | + return f"{self.message}\n{self.response}" |
| 146 | + |
| 147 | + def to_operation_outcome(self) -> dict: |
| 148 | + return create_operation_outcome( |
| 149 | + resource_id=str(uuid.uuid4()), |
| 150 | + severity=Severity.error, |
| 151 | + code=Code.server_error, |
| 152 | + diagnostics=self.__str__(), |
| 153 | + ) |
| 154 | + |
| 155 | + |
| 156 | +def create_operation_outcome(resource_id: str, severity: Severity, code: Code, diagnostics: str) -> dict: |
| 157 | + """Create an OperationOutcome object. Do not use `fhir.resource` library since it adds unnecessary validations""" |
| 158 | + return { |
| 159 | + "resourceType": "OperationOutcome", |
| 160 | + "id": resource_id, |
| 161 | + "meta": {"profile": ["https://simplifier.net/guide/UKCoreDevelopment2/ProfileUKCore-OperationOutcome"]}, |
| 162 | + "issue": [ |
| 163 | + { |
| 164 | + "severity": severity, |
| 165 | + "code": code, |
| 166 | + "details": { |
| 167 | + "coding": [ |
| 168 | + { |
| 169 | + "system": "https://fhir.nhs.uk/Codesystem/http-error-codes", |
| 170 | + "code": code.upper(), |
| 171 | + } |
| 172 | + ] |
| 173 | + }, |
| 174 | + "diagnostics": diagnostics, |
| 175 | + } |
| 176 | + ], |
| 177 | + } |
0 commit comments