Skip to content

Error Handling

Handling errors in an application is another must. The errors can be added into the middleware of any FastAPI application.


Table of Contents


http_error_handler

Example:

from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException
from python_web_extras.fastapi.errors.http_error import http_error_handler

app = FastAPI(__name__)

app.add_exception_handler(HTTPException, http_error_handler)

http422_error_handler

Example:

from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException
from python_web_extras.fastapi.errors.validation_error import http422_error_handler

app = FastAPI(__name__)

app.add_exception_handler(RequestValidationError, http422_error_handler)

Working example

An example of this approach can be found here.