diff --git a/core/app.py b/core/app.py index edd9a30..9dad183 100644 --- a/core/app.py +++ b/core/app.py @@ -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) diff --git a/core/base/exceptions.py b/core/base/exceptions.py index 8033ee3..64fc03a 100644 --- a/core/base/exceptions.py +++ b/core/base/exceptions.py @@ -23,6 +23,9 @@ class SearchCardRetry(ScraperError): """搜索重试""" pass +class SearchCardRetryFail(ScraperError): + """搜索重试失败""" + pass class WechatApiError(CardBookException): """微信开放服务调用api错误""" diff --git a/core/routers/card/scraper.py b/core/routers/card/scraper.py index 90c8521..df78a39 100644 --- a/core/routers/card/scraper.py +++ b/core/routers/card/scraper.py @@ -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,11 +60,19 @@ class SiteApi: 'http': settings.OVERSEAS_PROXY, 'https': settings.OVERSEAS_PROXY } - r = await self.session.request( - method, url, headers=headers, params=params, data=data, json=json, **kwargs - ) - logger.debug(f'耗时:{url} {r.elapsed}') - return r + + 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)