Skip to content

Commit 18f558f

Browse files
committed
NRL-708 initial exception handling
1 parent fff7efe commit 18f558f

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

api/producer/createDocumentReference/create_document_reference.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from uuid import uuid4
22

3+
from core.errors import OperationOutcomeError
4+
35
from nrlf.core.constants import PERMISSION_AUDIT_DATES_FROM_PAYLOAD
46
from nrlf.core.decorators import request_handler
57
from nrlf.core.dynamodb.repository import DocumentPointer, DocumentPointerRepository
@@ -74,10 +76,20 @@ def handler(
7476
document_reference=result.resource,
7577
nrl_permissions=metadata.nrl_permissions,
7678
)
77-
78-
core_model = DocumentPointer.from_document_reference(
79-
document_reference, created_on=creation_time
80-
)
79+
try:
80+
core_model = DocumentPointer.from_document_reference(
81+
document_reference, created_on=creation_time
82+
)
83+
except OperationOutcomeError as exc:
84+
return SpineErrorResponse.BAD_REQUEST(
85+
diagnostics=exc.diagnostics,
86+
expression="DocumentReference.type.coding",
87+
)
88+
except Exception as exc:
89+
return SpineErrorResponse.BAD_REQUEST(
90+
diagnostics=f"Parsing of Document Pointer has raised exception with message: '{str(exc)}'.",
91+
expression="DocumentReference",
92+
)
8193

8294
custodian_parts = tuple(
8395
filter(None, (core_model.custodian, core_model.custodian_suffix))

layer/nrlf/core/dynamodb/model.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from enum import Enum
44
from typing import Any, Dict, Optional
55

6+
from core.codes import SpineErrorConcept
7+
from core.errors import OperationOutcomeError
68
from nhs_number import is_valid as is_valid_nhs_number
79
from pydantic import BaseModel, Field, PrivateAttr, root_validator, validator
810

@@ -77,7 +79,13 @@ def from_document_reference(
7779
custodian = getattr(custodian_identifier, "value")
7880

7981
if len(coding) > 1:
80-
raise ValueError("DocumentReference.type.coding must have exactly one item")
82+
raise OperationOutcomeError(
83+
status_code="400",
84+
severity="error",
85+
code="invalid",
86+
details=SpineErrorConcept.from_code("INVALID_RESOURCE"),
87+
diagnostics="DocumentReference.type.coding must have exactly one item",
88+
) from None
8189

8290
pointer_type = f"{coding[0].system}|{coding[0].code}"
8391

0 commit comments

Comments
 (0)