File size: 676 Bytes
af35098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from fastapi import Request
from fastapi.responses import JSONResponse
from starlette.exceptions import HTTPException as StarletteHTTPException

async def custom_404_handler(request: Request, exc: StarletteHTTPException):
    """Custom handler to format 404 errors cleanly."""
    if exc.status_code == 404:
        return JSONResponse(
            status_code=404,
            content={
                "error": "Endpoint not found", 
                "path": request.url.path,
                "message": "Please check the URL or visit /docs for available endpoints."
            }
        )
    return JSONResponse(status_code=exc.status_code, content={"error": exc.detail})