🎨 Handle NetworkError and TimedOut exceptions separately

This commit is contained in:
洛水居室 2022-11-24 23:02:27 +08:00
parent 4d67904118
commit da0faf6d98
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC

View File

@ -6,7 +6,7 @@ import traceback
import aiofiles import aiofiles
from telegram import ReplyKeyboardRemove, Update from telegram import ReplyKeyboardRemove, Update
from telegram.constants import ParseMode from telegram.constants import ParseMode
from telegram.error import BadRequest, Forbidden from telegram.error import BadRequest, Forbidden, NetworkError, TimedOut
from telegram.ext import CallbackContext from telegram.ext import CallbackContext
from core.bot import bot from core.bot import bot
@ -32,6 +32,13 @@ class ErrorHandler(Plugin):
async def error_handler(self, update: object, context: CallbackContext) -> None: async def error_handler(self, update: object, context: CallbackContext) -> None:
"""记录错误并发送消息通知开发人员。 logger the error and send a telegram message to notify the developer.""" """记录错误并发送消息通知开发人员。 logger the error and send a telegram message to notify the developer."""
if isinstance(NetworkError, context.error):
logger.error("Bot请求异常", exc_info=context.error)
return
if isinstance(TimedOut, context.error):
logger.error("Bot请求超时", exc_info=context.error)
return
logger.error("处理函数时发生异常") logger.error("处理函数时发生异常")
logger.exception(context.error, exc_info=(type(context.error), context.error, context.error.__traceback__)) logger.exception(context.error, exc_info=(type(context.error), context.error, context.error.__traceback__))