beda-software / fhir-py

FHIR Client for python
MIT License
168 stars 31 forks source link

Only pass error message without formatting #75

Closed BSVogler closed 2 years ago

BSVogler commented 3 years ago

A typical error may contain addition information like formatted output (HAPI) and it will report as this in sentry:

{ "resourceType": "OperationOutcome", "text": { "status": "generated", "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">

Operation Outcome

<table border=\"0\"><td style=\"font-weight: bold;\">ERROR[]
Failed to apply JSON patch to Patient/d0d848c4-b917-11ea-aabf-86fddcb4ced6: [element="value"] Invalid attribute value "": Attribute value must not be empty ("")
\n\t\t\t\n\t\t\n\t
" }, "issue": [ { ...

This change only reports the error message as defined by the standard.

ruscoder commented 3 years ago

Hi @BSVogler! Thanks for the contribution.

Related issues: https://github.com/beda-software/fhir-py/issues/53, https://github.com/beda-software/fhir-py/issues/50.

Parsing json should be done closer to the code that raises OperationOutcome: https://github.com/beda-software/fhir-py/blob/0a22b8c358241ed0fca5dd427a28a6dabca12ef4/fhirpy/base/lib.py#L110 https://github.com/beda-software/fhir-py/blob/0a22b8c358241ed0fca5dd427a28a6dabca12ef4/fhirpy/base/lib.py#L133 https://github.com/beda-software/fhir-py/blob/0a22b8c358241ed0fca5dd427a28a6dabca12ef4/fhirpy/base/lib.py#L329 https://github.com/beda-software/fhir-py/blob/0a22b8c358241ed0fca5dd427a28a6dabca12ef4/fhirpy/base/lib.py#L387

And I like your idea to fetch the error from issue just using the code:

try:
    await resource.save()
except OperationOutcome  as e:
    print(e)

but the source OperationOutcome resource should be stored in OperationOutcome in attribute, for example as OperationOutcome.data, to give an ability to return it further or extract some details in the app's code:

try:
    await resource.save()
except OperationOutcome  as e:
    return web.json_response(e.data)
BSVogler commented 3 years ago

Both are reasonable suggestions. I will look into this.

ruscoder commented 2 years ago

We improved fhir server's OperationError response handling in https://github.com/beda-software/fhir-py/commit/f5afe66442a4eac89e6cad62b294d73106ebd7e5.

Now it is accessible via exc.resource:

try:
    await resource.save()
except OperationOutcome as exc:
    exc.resource # Here is the original OperationOutcome