Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python json_fhir_string_to_proto raises wrong exception type #29

Closed
gitpushdashf opened this issue Dec 15, 2020 · 2 comments
Closed

Python json_fhir_string_to_proto raises wrong exception type #29

gitpushdashf opened this issue Dec 15, 2020 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@gitpushdashf
Copy link

Hi everyone,

According to https://github.com/google/fhir/blame/master/py/google/fhir/r4/json_format.py#L82,L83 json_fhir_string_to_proto() should raise InvalidFhirError.

In this example, it raises ValueError instead. I have not tested with the latest google-fhir but am suspecting the results may be the same.

import json

from google.fhir.fhir_errors import InvalidFhirError
from google.fhir.r4.json_format import json_fhir_string_to_proto
from proto.google.fhir.proto.r4.core.resources.bundle_and_contained_resource_pb2 import (  # noqa: E501
    Bundle,
)

INVALID_BUNDLE_DICT = {
    "resourceType": "Bundle",
    "type": "not a transaction",
    "entry": [
        {
            "fullUrl": "urn:uuid:85e52038-4d69-50e9-9e46-e379b8d830af",
            "resource": {
                "resourceType": "Patient",
                "id": "85e52038-4d69-50e9-9e46-e379b8d830af",
                "name": [
                    {"use": "official", "family": "Simpson", "given": ["Homer", "Jay"]}
                ],
            },
        }
    ],
}

try:
    json_fhir_string_to_proto(json.dumps(INVALID_BUNDLE_DICT), Bundle)
except InvalidFhirError:
    print("Excepts as expected.")

print("Success.")

Let me know if anything is unclear. The error is under the type field. Happy to help dig in further if need be.

Thank you for your time.

@Cam2337
Copy link
Collaborator

Cam2337 commented Dec 16, 2020

Hey @gitpushdashf 👋

Within codes, we should be raising an instance of fhir_errors.InvalidFhirError, which would be more informative to the user that their provided code string cannot convert to an accompanying FHIRProto EnumValueDescriptor. Making this change properly throws an instance of fhir_errors.InvalidFhirError for your provided example (thank you for a great example btw 🙂). I merged this internally, so expect this fix in the next release.

As an aside, I've also filed a bug internally to review our error handling for google-fhir. I suspect there might be other places where a similar change should take place. I'm hoping to strike a good balance between raising ValueErrors (and other built-in exceptions) where it makes sense, and only relying on propagating fhir_errors.InvalidFhirError, where it's useful to let a caller know that "your JSON is correct, but it's not valid FHIR".

Thank you again!

@Cam2337 Cam2337 added the bug Something isn't working label Dec 16, 2020
@Cam2337 Cam2337 self-assigned this Dec 16, 2020
@gitpushdashf
Copy link
Author

You're welcome. Thank you for looking into this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants