2022-07-26 10:07:31 +00:00
|
|
|
|
import html
|
2022-09-08 01:08:37 +00:00
|
|
|
|
import json
|
2022-07-26 10:07:31 +00:00
|
|
|
|
import traceback
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from telegram import Update, ReplyKeyboardRemove
|
2022-07-26 10:07:31 +00:00
|
|
|
|
from telegram.constants import ParseMode
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from telegram.error import BadRequest, Forbidden
|
2022-07-26 10:07:31 +00:00
|
|
|
|
from telegram.ext import CallbackContext
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.bot import bot
|
|
|
|
|
from core.plugin import error_handler, Plugin
|
|
|
|
|
from utils.log import logger
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
notice_chat_id = bot.config.error_notification_chat_id
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
class ErrorHandler(Plugin):
|
2022-08-06 09:21:58 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
@error_handler(block=False) # pylint: disable=E1123, E1120
|
|
|
|
|
async def error_handler(self, update: object, context: CallbackContext) -> None:
|
|
|
|
|
"""记录错误并发送消息通知开发人员。 logger the error and send a telegram message to notify the developer."""
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error("处理函数时发生异常")
|
2022-09-10 04:02:19 +00:00
|
|
|
|
logger.exception(context.error, exc_info=(type(context.error), context.error, context.error.__traceback__))
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
if notice_chat_id is None:
|
2022-07-26 10:07:31 +00:00
|
|
|
|
return
|
2022-09-08 01:08:37 +00:00
|
|
|
|
|
|
|
|
|
tb_list = traceback.format_exception(None, context.error, context.error.__traceback__)
|
|
|
|
|
tb_string = ''.join(tb_list)
|
|
|
|
|
|
|
|
|
|
update_str = update.to_dict() if isinstance(update, Update) else str(update)
|
|
|
|
|
text_1 = (
|
|
|
|
|
f'<b>处理函数时发生异常</b> \n'
|
|
|
|
|
f'Exception while handling an update \n'
|
|
|
|
|
f'<pre>update = {html.escape(json.dumps(update_str, indent=2, ensure_ascii=False))}'
|
|
|
|
|
'</pre>\n\n'
|
|
|
|
|
f'<pre>context.chat_data = {html.escape(str(context.chat_data))}</pre>\n\n'
|
|
|
|
|
f'<pre>context.user_data = {html.escape(str(context.user_data))}</pre>\n\n'
|
|
|
|
|
)
|
|
|
|
|
text_2 = (
|
|
|
|
|
f'<pre>{html.escape(tb_string)}</pre>'
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
if 'make sure that only one bot instance is running' in tb_string:
|
|
|
|
|
logger.error("其他机器人在运行,请停止!")
|
|
|
|
|
return
|
|
|
|
|
await context.bot.send_message(notice_chat_id, text_1, parse_mode=ParseMode.HTML)
|
|
|
|
|
await context.bot.send_message(notice_chat_id, text_2, parse_mode=ParseMode.HTML)
|
|
|
|
|
except BadRequest as exc:
|
|
|
|
|
if 'too long' in str(exc):
|
2022-07-26 10:07:31 +00:00
|
|
|
|
text = (
|
2022-09-08 01:08:37 +00:00
|
|
|
|
f'<b>处理函数时发生异常,traceback太长导致无法发送,但已写入日志</b> \n'
|
|
|
|
|
f'<code>{html.escape(str(context.error))}</code>'
|
|
|
|
|
)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
try:
|
|
|
|
|
await context.bot.send_message(notice_chat_id, text, parse_mode=ParseMode.HTML)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
except BadRequest:
|
|
|
|
|
text = (
|
|
|
|
|
'<b>处理函数时发生异常,traceback太长导致无法发送,但已写入日志</b> \n')
|
|
|
|
|
try:
|
|
|
|
|
await context.bot.send_message(notice_chat_id, text, parse_mode=ParseMode.HTML)
|
|
|
|
|
except BadRequest as exc_1:
|
2022-09-10 04:02:19 +00:00
|
|
|
|
logger.exception(exc_1)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
effective_user = update.effective_user
|
|
|
|
|
effective_message = update.effective_message
|
|
|
|
|
try:
|
|
|
|
|
if effective_message is not None:
|
|
|
|
|
chat = effective_message.chat
|
|
|
|
|
logger.info(f"尝试通知用户 {effective_user.full_name}[{effective_user.id}] "
|
|
|
|
|
f"在 {chat.full_name}[{chat.id}]"
|
|
|
|
|
f"的 update_id[{update.update_id}] 错误信息")
|
|
|
|
|
text = "出错了呜呜呜 ~ 派蒙这边发生了点问题无法处理!"
|
|
|
|
|
await context.bot.send_message(effective_message.chat_id, text, reply_markup=ReplyKeyboardRemove(),
|
|
|
|
|
parse_mode=ParseMode.HTML)
|
|
|
|
|
except (BadRequest, Forbidden) as exc:
|
|
|
|
|
logger.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为")
|
|
|
|
|
logger.exception(exc)
|