HTTPException: Most General Solution

1
2
class HTTPException:
    def __init__(self, status_code: int, detail: Any = None, headers: dict[str, str] = None): ...

Default exception handler of HTTPException will return:

1
2
3
{
    "detail": ...
}

Custom Exception Handler

Register exception handler to the app, or override the default exception handler.

1
2
3
4
5
6
7
from fastapi import Request
from fastapi.responses import JSONResponse

class CustomException(BaseException): ...

@app.exception_handler(CustomException)
async def custom_exception_handler_example(request: Request, exc: CustomException) -> JSONResponse: ...


Back