Skip to content

Commit 542431d

Browse files
authored
ELI-318: Adds content-type in 404 and 500 error response (#232)
1 parent 63f3303 commit 542431d

4 files changed

Lines changed: 33 additions & 26 deletions

File tree

src/eligibility_signposting_api/error_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ def handle_exception(e: Exception) -> ResponseReturnValue | HTTPException:
2121
response = INTERNAL_SERVER_ERROR.log_and_generate_response(
2222
log_message=f"An unexpected error occurred: {full_traceback}", diagnostics="An unexpected error occurred."
2323
)
24-
return make_response(response.get("body"), response.get("statusCode"))
24+
return make_response(response.get("body"), response.get("statusCode"), response.get("headers"))

src/eligibility_signposting_api/views/eligibility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def handle_unknown_person_error(nhs_number: NHSNumber) -> ResponseReturnValue:
100100
response = NHS_NUMBER_NOT_FOUND_ERROR.log_and_generate_response(
101101
log_message=diagnostics, diagnostics=diagnostics, location_param="id"
102102
)
103-
return make_response(response.get("body"), response.get("statusCode"))
103+
return make_response(response.get("body"), response.get("statusCode"), response.get("headers"))
104104

105105

106106
def build_eligibility_response(eligibility_status: EligibilityStatus) -> eligibility.EligibilityResponse:

tests/integration/lambda/test_app_running_as_lambda.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -124,36 +124,39 @@ def test_install_and_call_flask_lambda_with_unknown_nhs_number(
124124
timeout=10,
125125
)
126126

127-
# Then
127+
decoded_body_bytes = base64.b64decode(response.text)
128+
decoded_body_json = json.loads(decoded_body_bytes.decode("utf-8"))
129+
128130
assert_that(
129131
response,
130132
is_response()
131133
.with_status_code(HTTPStatus.NOT_FOUND)
132-
.and_body(
133-
is_json_that(
134+
.with_headers(has_entries({"Content-Type": "application/fhir+json"})),
135+
)
136+
137+
# Then
138+
assert_that(
139+
decoded_body_json,
140+
has_entries(
141+
resourceType=equal_to("OperationOutcome"),
142+
issue=contains_exactly(
134143
has_entries(
135-
resourceType="OperationOutcome",
136-
issue=contains_exactly(
137-
has_entries(
138-
severity="error",
139-
code="processing",
140-
diagnostics=f"NHS Number '{nhs_number!s}' was not "
141-
f"recognised by the Eligibility Signposting API",
142-
details={
143-
"coding": [
144-
{
145-
"system": "https://fhir.nhs.uk/STU3/ValueSet/Spine-ErrorOrWarningCode-1",
146-
"code": "REFERENCE_NOT_FOUND",
147-
"display": "The given NHS number was not found in our datasets. "
148-
"This could be because the number is incorrect or "
149-
"some other reason we cannot process that number.",
150-
}
151-
]
152-
},
153-
)
154-
),
144+
severity="error",
145+
code="processing",
146+
diagnostics=f"NHS Number '{nhs_number!s}' was not recognised by the Eligibility Signposting API",
147+
details={
148+
"coding": [
149+
{
150+
"system": "https://fhir.nhs.uk/STU3/ValueSet/Spine-ErrorOrWarningCode-1",
151+
"code": "REFERENCE_NOT_FOUND",
152+
"display": "The given NHS number was not found in our datasets. "
153+
"This could be because the number is incorrect or "
154+
"some other reason we cannot process that number.",
155+
}
156+
]
157+
},
155158
)
156-
)
159+
),
157160
),
158161
)
159162

@@ -292,6 +295,7 @@ def test_given_nhs_number_in_path_does_not_match_with_nhs_number_in_headers_resu
292295
response,
293296
is_response()
294297
.with_status_code(HTTPStatus.FORBIDDEN)
298+
.with_headers(has_entries({"Content-Type": "application/fhir+json"}))
295299
.and_body(
296300
is_json_that(
297301
has_entries(
@@ -338,6 +342,7 @@ def test_given_nhs_number_not_present_in_headers_results_in_error_response(
338342
response,
339343
is_response()
340344
.with_status_code(HTTPStatus.FORBIDDEN)
345+
.with_headers(has_entries({"Content-Type": "application/fhir+json"}))
341346
.and_body(
342347
is_json_that(
343348
has_entries(

tests/unit/views/test_eligibility.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def test_no_nhs_number_given(app: Flask, client: FlaskClient):
121121
response,
122122
is_response()
123123
.with_status_code(HTTPStatus.NOT_FOUND)
124+
.with_headers(has_entries({"Content-Type": "application/fhir+json"}))
124125
.and_text(
125126
is_json_that(
126127
has_entries(
@@ -158,6 +159,7 @@ def test_unexpected_error(app: Flask, client: FlaskClient):
158159
response,
159160
is_response()
160161
.with_status_code(HTTPStatus.INTERNAL_SERVER_ERROR)
162+
.with_headers(has_entries({"Content-Type": "application/fhir+json"}))
161163
.and_text(
162164
is_json_that(
163165
has_entries(

0 commit comments

Comments
 (0)