Skip to content

Commit f55a9a5

Browse files
committed
errors III
1 parent 587a0db commit f55a9a5

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

lambdas/shared/src/common/models/errors.py

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Severity(str, Enum):
1212
class Code(str, Enum):
1313
forbidden = "forbidden"
1414
not_found = "not-found"
15-
invalid = "invalid"
16-
server_error = "exception"
15+
invalid = "invalid or missing access token"
16+
server_error = "internal server error"
1717
invariant = "invariant"
1818
incomplete = "parameter-incomplete"
1919
not_supported = "not-supported"
@@ -43,6 +43,12 @@ def to_operation_outcome() -> dict:
4343

4444
@dataclass
4545
class UnauthorizedVaxError(RuntimeError):
46+
response: dict | str
47+
message: str
48+
49+
def __str__(self):
50+
return f"{self.message}\n{self.response}"
51+
4652
@staticmethod
4753
def to_operation_outcome() -> dict:
4854
msg = "Unauthorized request for vaccine type"
@@ -56,6 +62,12 @@ def to_operation_outcome() -> dict:
5662

5763
@dataclass
5864
class UnauthorizedVaxOnRecordError(RuntimeError):
65+
response: dict | str
66+
message: str
67+
68+
def __str__(self):
69+
return f"{self.message}\n{self.response}"
70+
5971
@staticmethod
6072
def to_operation_outcome() -> dict:
6173
msg = "Unauthorized request for vaccine type present in the stored immunization resource"
@@ -109,7 +121,7 @@ def to_operation_outcome() -> dict:
109121
class ResourceNotFoundError(RuntimeError):
110122
"""Return this error when the requested FHIR resource does not exist"""
111123

112-
resource_type: str
124+
resource_type: str | None
113125
resource_id: str
114126

115127
def __str__(self):
@@ -284,30 +296,6 @@ def to_operation_outcome(self) -> dict:
284296
)
285297

286298

287-
def create_operation_outcome(resource_id: str, severity: Severity, code: Code, diagnostics: str) -> dict:
288-
"""Create an OperationOutcome object. Do not use `fhir.resource` library since it adds unnecessary validations"""
289-
return {
290-
"resourceType": "OperationOutcome",
291-
"id": resource_id,
292-
"meta": {"profile": ["https://simplifier.net/guide/UKCoreDevelopment2/ProfileUKCore-OperationOutcome"]},
293-
"issue": [
294-
{
295-
"severity": severity,
296-
"code": code,
297-
"details": {
298-
"coding": [
299-
{
300-
"system": "https://fhir.nhs.uk/Codesystem/http-error-codes",
301-
"code": code.upper(),
302-
}
303-
]
304-
},
305-
"diagnostics": diagnostics,
306-
}
307-
],
308-
}
309-
310-
311299
@dataclass
312300
class ParameterException(RuntimeError):
313301
message: str
@@ -349,3 +337,27 @@ class RecordProcessorError(Exception):
349337

350338
def __init__(self, diagnostics_dictionary: dict):
351339
self.diagnostics_dictionary = diagnostics_dictionary
340+
341+
342+
def create_operation_outcome(resource_id: str, severity: Severity, code: Code, diagnostics: str) -> dict:
343+
"""Create an OperationOutcome object. Do not use `fhir.resource` library since it adds unnecessary validations"""
344+
return {
345+
"resourceType": "OperationOutcome",
346+
"id": resource_id,
347+
"meta": {"profile": ["https://simplifier.net/guide/UKCoreDevelopment2/ProfileUKCore-OperationOutcome"]},
348+
"issue": [
349+
{
350+
"severity": severity,
351+
"code": code,
352+
"details": {
353+
"coding": [
354+
{
355+
"system": "https://fhir.nhs.uk/Codesystem/http-error-codes",
356+
"code": code.upper(),
357+
}
358+
]
359+
},
360+
"diagnostics": diagnostics,
361+
}
362+
],
363+
}

0 commit comments

Comments
 (0)