🎨 优化全局报错提醒

This commit is contained in:
xtaodada 2022-10-23 00:40:30 +08:00
parent 291b21f674
commit 033eaa697a
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659

View File

@ -5,7 +5,7 @@ from typing import Callable, cast
from aiohttp import ClientConnectorError from aiohttp import ClientConnectorError
from genshin import InvalidCookies, GenshinException, TooManyRequests, DataNotPublic from genshin import InvalidCookies, GenshinException, TooManyRequests, DataNotPublic
from httpx import ConnectTimeout from httpx import ConnectTimeout
from telegram import Update, ReplyKeyboardRemove from telegram import Update, ReplyKeyboardRemove, InlineKeyboardButton
from telegram.error import BadRequest, TimedOut, Forbidden from telegram.error import BadRequest, TimedOut, Forbidden
from telegram.ext import CallbackContext, ConversationHandler from telegram.ext import CallbackContext, ConversationHandler
@ -14,9 +14,10 @@ from utils.error import UrlResourcesNotFoundError
from utils.log import logger from utils.log import logger
async def send_user_notification(update: Update, _: CallbackContext, text: str): async def send_user_notification(update: Update, context: CallbackContext, text: str):
if update.inline_query is not None: # 忽略 inline_query if update.inline_query is not None: # 忽略 inline_query
return return
buttons = [[InlineKeyboardButton("点我重新绑定", url=f"https://t.me/{context.bot.username}?start=set_cookie")]]
user = update.effective_user user = update.effective_user
message = update.effective_message message = update.effective_message
chat = update.effective_chat chat = update.effective_chat
@ -26,14 +27,10 @@ async def send_user_notification(update: Update, _: CallbackContext, text: str):
return return
logger.info(f"尝试通知用户 {user.full_name}[{user.id}] " f"{chat.full_name}[{chat.id}]" f"的 错误信息[{text}]") logger.info(f"尝试通知用户 {user.full_name}[{user.id}] " f"{chat.full_name}[{chat.id}]" f"的 错误信息[{text}]")
try: try:
await message.reply_text(text, reply_markup=ReplyKeyboardRemove(), allow_sending_without_reply=True) await message.reply_text(
except BadRequest as exc: text, reply_markup=buttons if "重新绑定" in text else ReplyKeyboardRemove(), allow_sending_without_reply=True
logger.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为") )
logger.exception(exc) except (BadRequest, Forbidden, Exception) as exc:
except Forbidden as exc:
logger.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为")
logger.exception(exc)
except Exception as exc:
logger.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为") logger.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为")
logger.exception(exc) logger.exception(exc)
finally: finally:
@ -70,15 +67,15 @@ def error_callable(func: Callable) -> Callable:
return await func(*args, **kwargs) return await func(*args, **kwargs)
except ClientConnectorError: except ClientConnectorError:
logger.error("aiohttp 模块连接服务器 ClientConnectorError") logger.error("aiohttp 模块连接服务器 ClientConnectorError")
await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ ") await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ 请稍后再试")
return ConversationHandler.END return ConversationHandler.END
except ConnectTimeout: except ConnectTimeout:
logger.error("httpx 模块连接服务器 ConnectTimeout") logger.error("httpx 模块连接服务器 ConnectTimeout")
await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ ") await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ 请稍后再试")
return ConversationHandler.END return ConversationHandler.END
except TimedOut: except TimedOut:
logger.error("python-telegram-bot 模块连接服务器 TimedOut") logger.error("python-telegram-bot 模块连接服务器 TimedOut")
await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ ") await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ 请稍后再试")
return ConversationHandler.END return ConversationHandler.END
except UrlResourcesNotFoundError as exc: except UrlResourcesNotFoundError as exc:
logger.error("URL数据资源未找到") logger.error("URL数据资源未找到")
@ -87,13 +84,13 @@ def error_callable(func: Callable) -> Callable:
return ConversationHandler.END return ConversationHandler.END
except InvalidCookies as exc: except InvalidCookies as exc:
if exc.retcode in (10001, -100): if exc.retcode in (10001, -100):
await send_user_notification(update, context, "出错了呜呜呜 ~ Cookies无效请尝试重新绑定账户") await send_user_notification(update, context, "出错了呜呜呜 ~ Cookie 无效,请尝试重新绑定")
elif exc.retcode == 10103: elif exc.retcode == 10103:
await send_user_notification(update, context, "出错了呜呜呜 ~ Cookie有效,但没有绑定到游戏帐户," "请尝试重新绑定邮游戏账户") await send_user_notification(update, context, "出错了呜呜呜 ~ Cookie 有效,但没有绑定到游戏帐户,请尝试重新绑定")
else: else:
logger.warning("Cookie错误") logger.warning("Cookie错误")
logger.exception(exc) logger.exception(exc)
await send_user_notification(update, context, f"出错了呜呜呜 ~ Cookies无效 错误信息为 {exc.msg}") await send_user_notification(update, context, f"出错了呜呜呜 ~ Cookie 无效 错误信息为 {exc.msg} 请尝试重新绑定")
return ConversationHandler.END return ConversationHandler.END
except TooManyRequests as exc: except TooManyRequests as exc:
logger.warning("查询次数太多(操作频繁)", exc) logger.warning("查询次数太多(操作频繁)", exc)
@ -104,22 +101,22 @@ def error_callable(func: Callable) -> Callable:
return ConversationHandler.END return ConversationHandler.END
except GenshinException as exc: except GenshinException as exc:
if exc.retcode == -130: if exc.retcode == -130:
await send_user_notification(update, context, "出错了呜呜呜 ~ 未设置默认角色,请尝试重新绑定默认角色") await send_user_notification(update, context, "出错了呜呜呜 ~ 未设置默认角色,请尝试重新绑定")
return ConversationHandler.END return ConversationHandler.END
logger.error("GenshinException") logger.error("GenshinException")
logger.exception(exc) logger.exception(exc)
await send_user_notification(update, context, f"出错了呜呜呜 ~ 获取账号信息发生错误 错误信息为 {exc.msg}") await send_user_notification(update, context, f"出错了呜呜呜 ~ 获取账号信息发生错误 错误信息为 {exc.msg} ~ 请稍后再试")
return ConversationHandler.END return ConversationHandler.END
except ReturnCodeError as exc: except ReturnCodeError as exc:
await send_user_notification(update, context, f"出错了呜呜呜 ~ API请求错误 错误信息为 {exc.message}") await send_user_notification(update, context, f"出错了呜呜呜 ~ API请求错误 错误信息为 {exc.message} ~ 请稍后再试")
return ConversationHandler.END return ConversationHandler.END
except APIHelperTimedOut: except APIHelperTimedOut:
logger.warning("APIHelperException") logger.warning("APIHelperException")
await send_user_notification(update, context, "出错了呜呜呜 ~ API请求超时") await send_user_notification(update, context, "出错了呜呜呜 ~ API请求超时 ~ 请稍后再试")
except APIHelperException as exc: except APIHelperException as exc:
logger.error("APIHelperException") logger.error("APIHelperException")
logger.exception(exc) logger.exception(exc)
await send_user_notification(update, context, "出错了呜呜呜 ~ API请求错误") await send_user_notification(update, context, "出错了呜呜呜 ~ API请求错误 ~ 请稍后再试")
return ConversationHandler.END return ConversationHandler.END
except BadRequest as exc: except BadRequest as exc:
if "Replied message not found" in exc.message: if "Replied message not found" in exc.message:
@ -131,12 +128,12 @@ def error_callable(func: Callable) -> Callable:
return ConversationHandler.END return ConversationHandler.END
logger.error("python-telegram-bot 请求错误") logger.error("python-telegram-bot 请求错误")
logger.exception(exc) logger.exception(exc)
await send_user_notification(update, context, "出错了呜呜呜 ~ telegram-bot-api请求错误") await send_user_notification(update, context, "出错了呜呜呜 ~ telegram-bot-api请求错误 ~ 请稍后再试")
return ConversationHandler.END return ConversationHandler.END
except Forbidden as exc: except Forbidden as exc:
logger.error("python-telegram-bot返回 Forbidden") logger.error("python-telegram-bot 返回 Forbidden")
logger.exception(exc) logger.exception(exc)
await send_user_notification(update, context, "出错了呜呜呜 ~ telegram-bot-api请求错误") await send_user_notification(update, context, "出错了呜呜呜 ~ telegram-bot-api请求错误 ~ 请稍后再试")
return ConversationHandler.END return ConversationHandler.END
return decorator return decorator