Spaces:
Paused

yaz / src /core /clinical_errors.py
anaselamr's picture
[PLATFORM] fix(endpoints+templates): Sprint 52 — zero-error operational pass
579285c
Raw History Blame Contribute Delete
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)
"""
@app.exception_handler(400)
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",
},
)
@app.exception_handler(404)
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",
},
)
@app.exception_handler(500)
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")