From 9ecc590c9de4518957bf1c0cec4f707b14a68263 Mon Sep 17 00:00:00 2001 From: aaron <> Date: Tue, 25 Feb 2025 09:42:15 +0800 Subject: [PATCH] update --- app/core/config.py | 3 +++ app/main.py | 34 +++++++++++++++++++++++----------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/app/core/config.py b/app/core/config.py index 7e3cabf..afb7678 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -8,6 +8,9 @@ class Settings(BaseSettings): API_BASE_URL: str = "https://api-dev.beefast.co" + # 企业微信机器人系统异常通知 + URL_WECOMBOT_SYS_EXCEPTION : str = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=edcc54c5-c352-42dd-b9be-0cc5b39cc0dc" + # 积分别名 POINT_ALIAS: str = "蜂蜜" POINT_RATIO: float = 10.0 # 积分兑换比例 diff --git a/app/main.py b/app/main.py index f134864..6d1a9de 100644 --- a/app/main.py +++ b/app/main.py @@ -9,6 +9,8 @@ from fastapi import HTTPException from app.middleware.request_logger import RequestLoggerMiddleware from app.core.response import CustomJSONResponse import logging +from app.core.config import settings +from app.core.wecombot import WecomBot # 创建数据库表 Base.metadata.create_all(bind=engine) @@ -70,18 +72,28 @@ async def root(): async def health_check(): return {"status": "healthy"} -@app.exception_handler(RequestValidationError) -async def validation_exception_handler(request, exc): - logging.exception(f"请求参数错误: {str(exc)}") - return CustomJSONResponse( - status_code=400, - content=str(exc) - ) -@app.exception_handler(HTTPException) -async def http_exception_handler(request, exc): - logging.exception(f"HTTP异常: {str(exc)}") - +@app.exception_handler(Exception) +async def exception_handler(request, exc): + logging.exception(f"API异常: {str(exc)}") + + # 发送企业微信消息 + wecom_bot = WecomBot(settings.URL_WECOMBOT_SYS_EXCEPTION) + + + exception_log = f"""**API异常** +**请求信息** +> 请求方法:{request.method} +> 请求URL:{request.url} +> 请求头:{request.headers} +> 请求体:{request.body} + +**异常信息** +{str(exc)} +""" + + await wecom_bot.send_markdown(exception_log) + return CustomJSONResponse( status_code=exc.status_code, content=str(exc)