Skip to content

Commit 329056c

Browse files
authored
BB2-2063 Update FHIR error handling to return new 400s from BFD w/ diagnostics (#1088)
* Update FHIR error handling to return new 400s from BFD w/ diagnostics * Fix minor Linting error * BB2-2063 Fix integration tests assertion
1 parent efdc585 commit 329056c

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

apps/fhir/bluebutton/exceptions.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from rest_framework.exceptions import NotFound, APIException
22
from rest_framework import status
33
from requests import Response
4+
from requests.exceptions import JSONDecodeError
45
from .models import Fhir_Response
56

67

78
def process_error_response(response: Fhir_Response) -> APIException:
89
"""
9-
TODO: This should be more specific (original comment before BB2-128)
10-
BB2-128: check FHIR response: if error is "IllegalArgumentException: Unsupported ID pattern..."
11-
then append the error to the general error message: 'An error occurred contacting the upstream server'
12-
As of BB2-291 in support BFD v2, it's a good time to map BFD 500 (server error) response where diagnostics contains
13-
java.lang.IllegalArgumentException to 'client error' 400 Bad Request, this will reduce large number of 500 (server error)
14-
alerts at runtime
10+
Process errors coming from FHIR endpoints.
11+
All status codes in 400s except 404 get wrapped in
12+
BadRequestError with diagnostics message attached if found.
13+
If not found, Generic UpstreamServerException will be returned.
14+
404 returns NotFound error.
15+
500 messages will return generic message with no diagnostics.
1516
"""
1617
err: APIException = None
1718
r: Response = response.backend_response
@@ -21,18 +22,16 @@ def process_error_response(response: Fhir_Response) -> APIException:
2122
else:
2223
msg = 'An error occurred contacting the upstream server'
2324
err = UpstreamServerException(msg)
24-
if response.status_code in [400, 500]:
25+
if response.status_code in range(400, 500):
2526
try:
2627
json = r.json()
2728
if json is not None:
2829
issues = json.get('issue')
2930
issue = issues[0] if issues else None
3031
diagnostics = issue.get('diagnostics') if issue else None
31-
if diagnostics is not None and "Unsupported ID pattern" in diagnostics:
32+
if diagnostics is not None:
3233
err = BadRequestToBackendError("{}:{}".format(msg, diagnostics))
33-
else:
34-
err = UpstreamServerException("{}:{}".format(msg, diagnostics))
35-
except Exception:
34+
except JSONDecodeError:
3635
pass
3736
return err
3837

apps/fhir/bluebutton/tests/test_fhir_resources_read_search_w_validation.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -508,21 +508,17 @@ def catchall(url, req):
508508
self.assertEqual(response.status_code, 200)
509509
self.assertEqual(response.json()["sub"], response.json()["patient"])
510510

511-
def test_err_response_caused_by_illegalarguments(self):
512-
self._err_response_caused_by_illegalarguments(False)
511+
def test_err_response_status_will_return_400_for_40x(self):
512+
# 401 will also return 400
513+
self._err_response_caused_by_illegalarguments(False, 401)
513514

514-
def test_err_response_caused_by_illegalarguments_bfd400(self):
515-
# BB2-1965 for 400 or 500 BFD response compatibility.
516-
self._err_response_caused_by_illegalarguments(False, 400)
515+
def test_err_response_404_will_return_4o4(self):
516+
self._err_response_caused_by_illegalarguments(False, 404, 404)
517517

518-
def test_err_response_caused_by_illegalarguments_v2(self):
519-
self._err_response_caused_by_illegalarguments(True)
518+
def test_err_response_500_will_return_502(self):
519+
self._err_response_caused_by_illegalarguments(False, 500, 502)
520520

521-
def test_err_response_caused_by_illegalarguments_v2_bfd400(self):
522-
# BB2-1965 for 400 or 500 BFD response compatibility.
523-
self._err_response_caused_by_illegalarguments(True, 400)
524-
525-
def _err_response_caused_by_illegalarguments(self, v2=False, bfd_status_code=500):
521+
def _err_response_caused_by_illegalarguments(self, v2=False, bfd_status_code=500, expected_code=400):
526522
# create the user
527523
first_access_token = self.create_token('John', 'Smith')
528524

@@ -543,4 +539,4 @@ def catchall(url, req):
543539
{'hello': 'world'},
544540
Authorization="Bearer %s" % (first_access_token))
545541

546-
self.assertEqual(response.status_code, 400)
542+
self.assertEqual(response.status_code, expected_code)

apps/integration_tests/integration_test_fhir_resources.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -744,8 +744,6 @@ def _err_response_caused_by_illegalarguments(self, v2=False):
744744
# Authenticate
745745
self._setup_apiclient(client)
746746
response = client.get(self._get_fhir_url(FHIR_RES_TYPE_COVERAGE, "part-d___--20140000008325", v2))
747-
# check that bfd error response 500 with root cause 'IllegalArgument'
748-
# mapped to 502 bad request (client error) currently.
749-
# NOTE: This will change back to 400 after BB2-2063 work.
747+
# This now returns 400 after BB2-2063 work.
750748
# for both v1 and v2
751-
self.assertEqual(response.status_code, 502)
749+
self.assertEqual(response.status_code, 400)

0 commit comments

Comments
 (0)