|
45 | 45 | WebSocket,
|
46 | 46 | WebSocketDisconnect,
|
47 | 47 | )
|
48 |
| -from fastapi.exceptions import RequestValidationError |
49 | 48 | from fastapi.background import BackgroundTasks
|
| 49 | +from fastapi.exception_handlers import request_validation_exception_handler as fastapi_default_validation_handler |
| 50 | +from fastapi.exceptions import RequestValidationError |
50 | 51 | from fastapi.middleware.cors import CORSMiddleware
|
51 | 52 | from fastapi.responses import JSONResponse, RedirectResponse, StreamingResponse
|
52 | 53 | from fastapi.staticfiles import StaticFiles
|
@@ -293,20 +294,37 @@ async def validation_exception_handler(_request: Request, exc: ValidationError):
|
293 | 294 |
|
294 | 295 | @app.exception_handler(RequestValidationError)
|
295 | 296 | async def request_validation_exception_handler(_request: Request, exc: RequestValidationError):
|
296 |
| - """Handle FastAPI request validation errors globally. |
| 297 | + """Handle FastAPI request validation errors (automatic request parsing). |
297 | 298 |
|
298 |
| - Intercepts RequestValidationError exceptions raised by FastAPI's automatic |
299 |
| - request validation and returns a properly formatted JSON error response. |
300 |
| - This ensures that user input is not reflected back in error messages. |
| 299 | + This handles ValidationErrors that occur during FastAPI's automatic request |
| 300 | + parsing before the request reaches your endpoint. |
301 | 301 |
|
302 | 302 | Args:
|
303 |
| - _request: The FastAPI request object that triggered the validation error. |
304 |
| - exc: The FastAPI RequestValidationError exception. |
| 303 | + _request: The FastAPI request object that triggered validation error. |
| 304 | + exc: The RequestValidationError exception containing failure details. |
305 | 305 |
|
306 | 306 | Returns:
|
307 |
| - JSONResponse: A 422 Unprocessable Entity response with sanitized error details. |
308 |
| - """ |
309 |
| - return JSONResponse(status_code=422, content=ErrorFormatter.format_validation_error(exc)) |
| 307 | + JSONResponse: A 422 Unprocessable Entity response with error details. |
| 308 | + """ |
| 309 | + if _request.url.path.startswith("/tools"): |
| 310 | + error_details = [] |
| 311 | + |
| 312 | + for error in exc.errors(): |
| 313 | + loc = error.get("loc", []) |
| 314 | + msg = error.get("msg", "Unknown error") |
| 315 | + ctx = error.get("ctx", {"error": {}}) |
| 316 | + type_ = error.get("type", "value_error") |
| 317 | + # Ensure ctx is JSON serializable |
| 318 | + if isinstance(ctx, dict): |
| 319 | + ctx_serializable = {k: (str(v) if isinstance(v, Exception) else v) for k, v in ctx.items()} |
| 320 | + else: |
| 321 | + ctx_serializable = str(ctx) |
| 322 | + error_detail = {"type": type_, "loc": loc, "msg": msg, "ctx": ctx_serializable} |
| 323 | + error_details.append(error_detail) |
| 324 | + |
| 325 | + response_content = {"detail": error_details} |
| 326 | + return JSONResponse(status_code=422, content=response_content) |
| 327 | + return await fastapi_default_validation_handler(_request, exc) |
310 | 328 |
|
311 | 329 |
|
312 | 330 | @app.exception_handler(IntegrityError)
|
|
0 commit comments