Problem
POST /api/v1/facility/ crashes with 500 Internal Server Error when an
invalid facility_type string is passed (e.g. "private" instead of "Private Hospital").
Root Cause
care/emr/resources/facility/spec.py line 153 does a direct dictionary
lookup with no prior validation:
obj.facility_type = REVERSE_REVERSE_FACILITY_TYPES[self.facility_type]
An invalid value throws a KeyError which propagates as a 500 instead of
being caught and returned as a 400.
Fix
Added a Pydantic field_validator on facility_type in FacilityCreateSpec
that validates the value before it reaches the lookup. Invalid values now
return a 400 with a message listing all valid options.
Before: 500 Internal Server Error — KeyError: 'private'
After: 400 Bad Request — "Invalid facility type. Valid options are: ..."
Only file changed: care/emr/resources/facility/spec.py
I have a fix ready and would like to submit a PR for this.