Performance: Custom error response models
MEDIUM IMPACT
This affects the server response time and payload size, impacting how quickly error information is delivered and rendered in the client.
from fastapi import FastAPI from fastapi.responses import JSONResponse from pydantic import BaseModel app = FastAPI() class ErrorResponse(BaseModel): error_code: int message: str @app.get("/items/{item_id}") async def read_item(item_id: int): if item_id == 0: return JSONResponse(status_code=404, content=ErrorResponse(error_code=404, message="Item not found").dict()) return {"item_id": item_id}
from fastapi import FastAPI, HTTPException app = FastAPI() @app.get("/items/{item_id}") async def read_item(item_id: int): if item_id == 0: raise HTTPException(status_code=404, detail="Item not found") return {"item_id": item_id}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Default HTTPException with string detail | N/A (API response only) | N/A | Minimal paint cost on client | [!] OK |
| Custom error response model with Pydantic | N/A (API response only) | N/A | Slightly higher paint cost due to larger payload | [OK] Good |