""" 全局错误处理器 """ import logging import traceback from fastapi import Request, status from fastapi.responses import JSONResponse from fastapi.exceptions import RequestValidationError from sqlalchemy.exc import SQLAlchemyError from app.core.exceptions import BaseAPIException logger = logging.getLogger(__name__) async def validation_exception_handler(request: Request, exc: RequestValidationError): """处理验证错误""" errors = [] for error in exc.errors(): field = ".".join(str(loc) for loc in error.get("loc", [])) errors.append({ "field": field, "message": error.get("msg"), "type": error.get("type") }) logger.warning(f"验证错误: {errors}") return JSONResponse( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, content={ "error": "VALIDATION_ERROR", "message": "请求参数验证失败", "details": errors } ) async def api_exception_handler(request: Request, exc: BaseAPIException): """处理自定义API异常""" logger.error(f"API异常: {exc.detail} (错误码: {exc.error_code})") return JSONResponse( status_code=exc.status_code, content={ "error": exc.error_code or "API_ERROR", "message": exc.detail } ) async def sqlalchemy_exception_handler(request: Request, exc: SQLAlchemyError): """处理数据库错误""" logger.error(f"数据库错误: {str(exc)}", exc_info=True) return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={ "error": "DATABASE_ERROR", "message": "数据库操作失败,请稍后重试" } ) async def general_exception_handler(request: Request, exc: Exception): """处理通用异常""" logger.error(f"未处理的异常: {str(exc)}", exc_info=True) logger.error(f"异常堆栈: {traceback.format_exc()}") return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={ "error": "INTERNAL_ERROR", "message": "服务器内部错误,请稍后重试" } )