API exception handling

List of content:

Description:

The FHIR standard specifies the specific format of errors responses and API outcomes.

The Operation outcomes are sets of error, warning and information messages that provide detailed information about the outcome of an attempted system operation. They are provided as a direct system response or component of one and provide information about the outcome of the operation.

More information can be found here: https://www.hl7.org/fhir/STU3/operationoutcome.html



The Python Django provides the possibility to create the custom exception handling which can be used to handle the specific API exceptions. In order to add custom exception handling the application configuration for REST need to be changed. 

More information can be found here:

https://www.django-rest-framework.org/api-guide/exceptions/

Implementation details:

In order to add custom exception handling the application configuration for REST need to be changed. The API FHIR module shouldn't just override configuration because of that configuration is shared by all modules. To avoid the situation when FHIR module override configuration provided by another module (for instance OpenIMIS REST module) the following algorithm is used:

  1. While the API FHIR module is loading:

    1. check if currently exist custom error handler in the configuration

      1. if exist save a pointer to that function in the local configuration of the module

    2. override the global configuration for REST and setting the local custom exception handler

  2. While handling the API exception:

    1. call the default handler:

      1. if exist handler saved in 1.a. then this handler is called

      2. if not then the default handler provided by Django REST (rest_framework.views.exception_handler) is called

    2. if the request was related to API FHIR then the module transform the response to FHIR OperationOutcome resource

FHIR resources:

https://www.hl7.org/fhir/STU3/operationoutcome.html

Module configuration used by the API exception handling: 

Configuration key

Description

Default value

Configuration key

Description

Default value

stu3_fhir_issue_type_config

configuration of system and codes used to represent the specific codes of OperationOutcome

"stu3_fhir_issue_type_config":{
  "fhir_code_for_exception":"exception",
  "fhir_code_for_not_found":"not-found"
}

Examples JSON representation of content:

Http404 exception:

{ "resourceType":"OperationOutcome", "issue":[ { "code":"not-found", "severity":"error" } ] }

FHIRException:

{ "resourceType": "OperationOutcome", "issue": [ { "code": "exception", "details": { "text": "The attribute named `invalid_attr` is not a valid property for `Patient`." }, "severity": "error" } ] }

KeyError:

{ "resourceType": "OperationOutcome", "issue": [ { "code": "exception", "details": { "text": "resourceType is missing" }, "severity": "error" } ] }

ValueError:



Did you encounter a problem or do you have a suggestion?

Please contact our Service Desk



This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. https://creativecommons.org/licenses/by-sa/4.0/