diff --git a/utils/decorators/error.py b/utils/decorators/error.py index 012c343..2d48a27 100644 --- a/utils/decorators/error.py +++ b/utils/decorators/error.py @@ -10,6 +10,7 @@ from telegram.error import BadRequest, TimedOut, Forbidden from telegram.ext import CallbackContext, ConversationHandler from logger import Log +from utils.error import UrlResourcesNotFoundError async def send_user_notification(update: Update, _: CallbackContext, text: str): @@ -73,8 +74,11 @@ def error_callable(func: Callable) -> Callable: Log.error("python-telegram-bot 模块连接服务器 TimedOut") await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ ") return ConversationHandler.END + except UrlResourcesNotFoundError as exc: + Log.error("URL数据资源未找到", exc) + await send_user_notification(update, context, "出错了呜呜呜 ~ 资源未找到 ~ ") + return ConversationHandler.END except InvalidCookies as exc: - Log.warning("Cookie错误", exc) if "[10001]" in str(exc): await send_user_notification(update, context, "Cookies无效,请尝试重新绑定账户") elif "[-100]" in str(exc): @@ -82,6 +86,7 @@ def error_callable(func: Callable) -> Callable: elif "[10103]" in str(exc): await send_user_notification(update, context, "Cookie有效,但没有绑定到游戏帐户,请尝试重新绑定邮游戏账户") else: + Log.warning("Cookie错误", exc) await send_user_notification(update, context, "Cookies无效,具体原因未知") return ConversationHandler.END except TooManyRequests as exc: diff --git a/utils/error.py b/utils/error.py index 5f3bc3a..f29729a 100644 --- a/utils/error.py +++ b/utils/error.py @@ -12,3 +12,8 @@ class NotFoundError(Exception): class RegionNotFoundError(NotFoundError): entity_name: str = "RegionEnum" entity_value_name: str = "region" + + +class UrlResourcesNotFoundError(NotFoundError): + entity_name: str = "url resources" + entity_value_name: str = "url" diff --git a/utils/helpers.py b/utils/helpers.py index c53f0d1..654c001 100644 --- a/utils/helpers.py +++ b/utils/helpers.py @@ -12,6 +12,7 @@ from core.cookies.services import CookiesService from core.user.services import UserService from logger import Log from models.base import RegionEnum +from utils.error import UrlResourcesNotFoundError USER_AGENT: str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " \ "Chrome/90.0.4430.72 Safari/537.36" @@ -54,9 +55,13 @@ async def url_to_file(url: str, prefix: str = "file://") -> str: return "" if data.is_error: Log.error(f"请求出现错误 url[{url}] status_code[{data.status_code}]") - return "" + raise UrlResourcesNotFoundError(url) + if data.status_code != 200: + Log.error(f"url_to_file 获取url[{url}] 错误 status_code[f{data.status_code}]") + raise UrlResourcesNotFoundError(url) async with aiofiles.open(file_dir, mode='wb') as f: await f.write(data.content) + Log.debug(f"url_to_file 获取url[{url}] 并下载到 file_dir[{file_dir}]") return prefix + file_dir