feat(card): 增加请求重试,异常错误提醒

This commit is contained in:
xingc
2025-04-17 18:15:49 +08:00
parent 862ae8fcca
commit f216d694d0
3 changed files with 33 additions and 9 deletions

View File

@@ -6,13 +6,13 @@
# @Desc :
import logging
import redis.asyncio as aioredis
from fastapi import FastAPI, Request, Response, status
from fastapi import FastAPI, Request, status
from fastapi.responses import UJSONResponse
from fastapi.exceptions import HTTPException, RequestValidationError
from fastapi_limiter import FastAPILimiter
from config import settings
from core.base.exceptions import CardBookException
from core.db.engine import get_cache
from core.routers.route import routers
from core.routers.chat.handlers import socket_manager
@@ -76,6 +76,18 @@ def create_app() -> FastAPI:
}
)
@app.exception_handler(CardBookException)
async def http_exception_handler(request: Request, exc: Exception):
await exception_log(request, exc)
return UJSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={
'status': status.HTTP_500_INTERNAL_SERVER_ERROR,
'data': None,
'message': str(exc)
}
)
@app.exception_handler(Exception)
async def http_exception_handler(request: Request, exc: Exception):
await exception_log(request, exc)

View File

@@ -23,6 +23,9 @@ class SearchCardRetry(ScraperError):
"""搜索重试"""
pass
class SearchCardRetryFail(ScraperError):
"""搜索重试失败"""
pass
class WechatApiError(CardBookException):
"""微信开放服务调用api错误"""

View File

@@ -7,13 +7,13 @@
import logging
import re
from curl_cffi import requests
from curl_cffi import requests, CurlError
from fake_useragent import UserAgent
from parsel import Selector
from simple_spider_tool import jsonpath, regx_match, format_json
from config import settings
from core.base.exceptions import SearchCardNotFound, SearchCardRetry
from core.base.exceptions import SearchCardNotFound, SearchCardRetry, SearchCardRetryFail
from core.utils.helper import async_retry
logger = logging.getLogger(__name__)
@@ -46,6 +46,7 @@ class SiteApi:
data=None,
json=None,
use_abroad_proxy: bool = False,
retry_num: int = 3,
**kwargs
):
headers = {
@@ -59,12 +60,20 @@ class SiteApi:
'http': settings.OVERSEAS_PROXY,
'https': settings.OVERSEAS_PROXY
}
for i in range(retry_num):
try:
r = await self.session.request(
method, url, headers=headers, params=params, data=data, json=json, **kwargs
)
logger.debug(f'耗时:{url} {r.elapsed}')
return r
except CurlError as e:
logger.error(e)
if i == retry_num - 1:
raise SearchCardRetryFail('请重试,如多次无效反馈管理员')
async def get(self, url, headers=None, params=None, **kwargs):
return await self.request('GET', url, headers=headers, params=params, **kwargs)