link
fastapi/exception-handler

Custom Exception Handler

Handle exceptions globally

error
handling

Command

Examples

Custom HTTP exception handler

from fastapi import FastAPI, HTTPException
  
  @app.exception_handler(HTTPException)
  async def http_exception_handler(request, exc):
      return JSONResponse(
          status_code=exc.status_code,
          content={"detail": exc.detail},
      )