feat: enhance exception handling with additional handlers for HTTP and validation errors

This commit is contained in:
shiyu
2025-12-08 21:21:57 +08:00
parent 8f515aaaf4
commit 394c2f7229
2 changed files with 55 additions and 6 deletions

14
main.py
View File

@@ -5,8 +5,15 @@ from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from db.session import close_db, init_db
from api.routers import include_routers
from fastapi import FastAPI
from middleware.exception_handler import global_exception_handler
from fastapi import FastAPI, HTTPException
from fastapi.exceptions import RequestValidationError
from middleware.exception_handler import (
global_exception_handler,
http_exception_handler,
httpx_exception_handler,
validation_exception_handler,
)
import httpx
from dotenv import load_dotenv
from domain.tasks.task_queue import task_queue_service
@@ -34,6 +41,9 @@ def create_app() -> FastAPI:
lifespan=lifespan,
)
include_routers(app)
app.add_exception_handler(HTTPException, http_exception_handler)
app.add_exception_handler(RequestValidationError, validation_exception_handler)
app.add_exception_handler(httpx.HTTPStatusError, httpx_exception_handler)
app.add_exception_handler(Exception, global_exception_handler)
return app