From a653a255623da93a30b4aa52ac4a91ff7dea3427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Sun, 11 Sep 2022 21:15:01 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20=E4=BF=AE=E6=94=B9=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E6=8A=A5=E5=91=8A=E7=9A=84=E5=BD=A2=E5=BC=8F=E4=B8=BA?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/system/errorhandler.py | 64 +++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/plugins/system/errorhandler.py b/plugins/system/errorhandler.py index be35447b..58ec8349 100644 --- a/plugins/system/errorhandler.py +++ b/plugins/system/errorhandler.py @@ -1,7 +1,9 @@ -import html import json +import os +import time import traceback +import aiofiles from telegram import Update, ReplyKeyboardRemove from telegram.constants import ParseMode from telegram.error import BadRequest, Forbidden @@ -12,6 +14,13 @@ from core.plugin import error_handler, Plugin from utils.log import logger notice_chat_id = bot.config.error_notification_chat_id +current_dir = os.getcwd() +logs_dir = os.path.join(current_dir, "logs") +if not os.path.exists(logs_dir): + os.mkdir(logs_dir) +report_dir = os.path.join(current_dir, "report") +if not os.path.exists(report_dir): + os.mkdir(report_dir) class ErrorHandler(Plugin): @@ -27,41 +36,38 @@ class ErrorHandler(Plugin): return tb_list = traceback.format_exception(None, context.error, context.error.__traceback__) - tb_string = ''.join(tb_list) + tb_string = "".join(tb_list) update_str = update.to_dict() if isinstance(update, Update) else str(update) - text_1 = ( - f'处理函数时发生异常 \n' - f'Exception while handling an update \n' - f'
update = {html.escape(json.dumps(update_str, indent=2, ensure_ascii=False))}'
-            '
\n\n' - f'
context.chat_data = {html.escape(str(context.chat_data))}
\n\n' - f'
context.user_data = {html.escape(str(context.user_data))}
\n\n' - ) - text_2 = ( - f'
{html.escape(tb_string)}
' + + error_text = ( + f"-----Exception while handling an update-----\n" + f"update = {json.dumps(update_str, indent=2, ensure_ascii=False)}\n" + f"context.chat_data = {str(context.chat_data)}\n" + f"context.user_data = {str(context.user_data)}\n" + "\n" + "-----Traceback info-----\n" + f"{tb_string}" ) + file_name = f"error_{update.update_id if isinstance(update, Update) else int(time.time())}.txt" + log_file = os.path.join(report_dir, file_name) + try: + async with aiofiles.open(log_file, mode='w+', encoding='utf-8') as f: + await f.write(error_text) + except Exception as exc: + logger.error("保存日记失败") + logger.exception(exc) 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): - text = ( - f'处理函数时发生异常,traceback太长导致无法发送,但已写入日志 \n' - f'{html.escape(str(context.error))}' - ) - try: - await context.bot.send_message(notice_chat_id, text, parse_mode=ParseMode.HTML) - except BadRequest: - text = ( - '处理函数时发生异常,traceback太长导致无法发送,但已写入日志 \n') - try: - await context.bot.send_message(notice_chat_id, text, parse_mode=ParseMode.HTML) - except BadRequest as exc_1: - logger.exception(exc_1) + await context.bot.send_document(chat_id=notice_chat_id, document=open(log_file, "rb"), + caption="Error report.") + except (BadRequest, Forbidden) as exc: + logger.error("发送日记失败") + logger.exception(exc) + except FileNotFoundError: + logger.error("发送日记失败 文件不存在") effective_user = update.effective_user effective_message = update.effective_message try: