Spaces:
Sleeping
Sleeping
File size: 5,123 Bytes
b64de39 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | """
Global Error Handler
Registers exception handlers on the FastAPI application so that:
1. Custom ``AppException`` subclasses are serialised into a consistent
JSON envelope with the correct HTTP status code.
2. Pydantic ``RequestValidationError`` returns a 422 with field-level
error details.
3. All unhandled exceptions are caught, logged with a stack trace, and
returned as a generic 500 response (no internal details leak).
"""
import logging
import traceback
from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from starlette.exceptions import HTTPException as StarletteHTTPException
from app.utils.exceptions import AppException
from app.utils.helpers import build_error_response
logger = logging.getLogger("farmhelp.errors")
def register_error_handlers(app: FastAPI) -> None:
"""Attach all exception handlers to the FastAPI application."""
# ------------------------------------------------------------------
# Custom application exceptions
# ------------------------------------------------------------------
@app.exception_handler(AppException)
async def app_exception_handler(request: Request, exc: AppException):
request_id = getattr(request.state, "request_id", None)
logger.warning(
"AppException | request_id=%s code=%s message=%s detail=%s",
request_id,
exc.error_code,
exc.message,
exc.detail,
)
body = build_error_response(
error_code=exc.error_code,
message=exc.message,
status_code=exc.status_code,
detail=exc.detail,
request_id=request_id,
)
return JSONResponse(status_code=exc.status_code, content=body)
# ------------------------------------------------------------------
# FastAPI / Starlette HTTP exceptions
# ------------------------------------------------------------------
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(
request: Request, exc: StarletteHTTPException
):
request_id = getattr(request.state, "request_id", None)
logger.warning(
"HTTPException | request_id=%s status=%s detail=%s",
request_id,
exc.status_code,
exc.detail,
)
detail_str = str(exc.detail) if exc.detail else "HTTP error"
body = build_error_response(
error_code="HTTP_ERROR",
message=detail_str,
status_code=exc.status_code,
request_id=request_id,
)
# Preserve top-level "detail" for backward compatibility with
# code that reads response.json()["detail"] directly.
body["detail"] = detail_str
return JSONResponse(status_code=exc.status_code, content=body)
# ------------------------------------------------------------------
# Pydantic validation errors
# ------------------------------------------------------------------
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
request: Request, exc: RequestValidationError
):
request_id = getattr(request.state, "request_id", None)
errors = exc.errors()
field_errors = []
for err in errors:
field_errors.append(
{
"field": " -> ".join(str(loc) for loc in err.get("loc", [])),
"message": err.get("msg", ""),
"type": err.get("type", ""),
}
)
logger.warning(
"ValidationError | request_id=%s errors=%s",
request_id,
field_errors,
)
body = build_error_response(
error_code="VALIDATION_ERROR",
message="Request validation failed.",
status_code=422,
detail=str(field_errors),
request_id=request_id,
)
body["validation_errors"] = field_errors
# Preserve top-level "detail" for backward compatibility.
body["detail"] = field_errors
return JSONResponse(status_code=422, content=body)
# ------------------------------------------------------------------
# Catch-all for unhandled exceptions
# ------------------------------------------------------------------
@app.exception_handler(Exception)
async def unhandled_exception_handler(request: Request, exc: Exception):
request_id = getattr(request.state, "request_id", None)
logger.error(
"UnhandledException | request_id=%s type=%s message=%s\n%s",
request_id,
type(exc).__name__,
str(exc),
traceback.format_exc(),
)
body = build_error_response(
error_code="INTERNAL_ERROR",
message="An unexpected error occurred. Please try again later.",
status_code=500,
request_id=request_id,
)
return JSONResponse(status_code=500, content=body)
|