Performance: Custom exception handlers
MEDIUM IMPACT
This affects server response time and user experience by controlling how errors are processed and returned.
from fastapi import FastAPI from fastapi.responses import JSONResponse app = FastAPI() @app.exception_handler(Exception) async def generic_exception_handler(request, exc): # Minimal processing, quick response return JSONResponse(status_code=500, content={"detail": "Internal Server Error"})
from fastapi import FastAPI from fastapi.responses import JSONResponse app = FastAPI() @app.exception_handler(Exception) async def generic_exception_handler(request, exc): # Heavy logging and complex processing here import time import asyncio await asyncio.sleep(2) # Simulate slow processing asynchronously return JSONResponse(status_code=500, content={"detail": "Internal Server Error"})
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy blocking exception handler | N/A | N/A | N/A | [X] Bad |
| Lightweight async exception handler | N/A | N/A | N/A | [OK] Good |