In Python, an exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. In the context of FastAPI, exceptions can occur due to various reasons such as invalid input data, database errors, or external service failures.
FastAPI has some built - in exception handlers, such as RequestValidationError
which is raised when the input data fails validation. However, for more complex scenarios, you may need to define your own custom exception handlers.
from fastapi import HTTPException
class CustomException(HTTPException):
def __init__(self, status_code: int, detail: str):
super().__init__(status_code=status_code, detail=detail)
In this example, we define a custom exception class CustomException
that inherits from HTTPException
. We can use this class to raise custom errors with a specific status code and error message.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
raise CustomException(status_code=400, detail="This is a custom error.")
In this route, we raise the CustomException
with a status code of 400 and a custom error message.
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
app = FastAPI()
class CustomException(HTTPException):
def __init__(self, status_code: int, detail: str):
super().__init__(status_code=status_code, detail=detail)
@app.exception_handler(CustomException)
def custom_exception_handler(request: Request, exc: CustomException):
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.detail}
)
@app.get("/")
def read_root():
raise CustomException(status_code=400, detail="This is a custom error.")
In this code, we register a custom exception handler for the CustomException
class. When a CustomException
is raised, the custom_exception_handler
function will be called to return a JSON response with the appropriate status code and error message.
from fastapi import FastAPI, Request, status
from fastapi.responses import JSONResponse
from pydantic import ValidationError
app = FastAPI()
@app.exception_handler(ValidationError)
def validation_exception_handler(request: Request, exc: ValidationError):
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content={"message": "Input validation failed", "errors": exc.errors()}
)
This code registers an exception handler for ValidationError
. When input data fails validation, a JSON response with a 422 status code and detailed error information will be returned.
import psycopg2
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
app = FastAPI()
@app.exception_handler(psycopg2.Error)
def database_exception_handler(request: Request, exc: psycopg2.Error):
return JSONResponse(
status_code=500,
content={"message": "Database error occurred", "detail": str(exc)}
)
Here, we register an exception handler for psycopg2.Error
(a PostgreSQL database error). When a database error occurs, a JSON response with a 500 status code and the error details will be returned.
It’s a good practice to centralize all your exception handlers in a separate module. This makes your code more organized and easier to maintain.
Error messages should be clear and concise. They should provide enough information for the user to understand what went wrong and how to fix it.
Logging exceptions can be very helpful for debugging. You can use Python’s built - in logging
module to log exceptions with additional information such as the request URL, input data, etc.
import logging
logging.basicConfig(level=logging.ERROR)
@app.exception_handler(CustomException)
def custom_exception_handler(request: Request, exc: CustomException):
logging.error(f"Custom exception occurred: {exc.detail} in request {request.url}")
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.detail}
)
Custom exception handling in FastAPI is a powerful feature that allows you to create more robust and user - friendly APIs. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can effectively handle exceptions in your FastAPI applications. Centralizing exception handling, providing clear error messages, and logging exceptions are key steps to ensure the reliability and maintainability of your API.