Download src/core/clinical_errors.py from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 1.96 kB
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/core/clinical_errors.py
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/core/clinical_errors.py
-
curl -L -o clinical_errors.py https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/core/clinical_errors.py
1.96 kB
| """ | |
| YAZ Care Platform - Clinical Error Handling | |
| Configures structured error responses for clinical endpoints | |
| with HIPAA-compliant error sanitization (no PHI in error responses). | |
| Standards: | |
| - RFC 7807 Problem Details | |
| - HIPAA §164.312 (error handling must not leak PHI) | |
| """ | |
| import structlog | |
| from typing import Any | |
| from fastapi import FastAPI, Request | |
| from fastapi.responses import JSONResponse | |
| logger = structlog.get_logger(__name__) | |
| def setup_clinical_error_handling(app: FastAPI) -> None: | |
| """ | |
| Register clinical-grade error handlers on the FastAPI app. | |
| - 400: Validation errors with sanitised details | |
| - 404: Resource not found (no ID leak) | |
| - 422: Unprocessable entity | |
| - 500: Generic server error (no stack traces in production) | |
| """ | |
| async def bad_request_handler(request: Request, exc: Any) -> JSONResponse: | |
| return JSONResponse( | |
| status_code=400, | |
| content={ | |
| "error": "bad_request", | |
| "message": "Invalid request data", | |
| "code": "ERR_BAD_REQUEST", | |
| }, | |
| ) | |
| async def not_found_handler(request: Request, exc: Any) -> JSONResponse: | |
| return JSONResponse( | |
| status_code=404, | |
| content={ | |
| "error": "not_found", | |
| "message": "Resource not found", | |
| "code": "ERR_NOT_FOUND", | |
| }, | |
| ) | |
| async def server_error_handler(request: Request, exc: Any) -> JSONResponse: | |
| logger.exception("Unhandled server error on %s", request.url.path) | |
| return JSONResponse( | |
| status_code=500, | |
| content={ | |
| "error": "internal_server_error", | |
| "message": "An unexpected error occurred", | |
| "code": "ERR_INTERNAL", | |
| }, | |
| ) | |
| logger.info("✅ Clinical error handling registered") | |