mirror of
https://github.com/PaiGramTeam/PamGram.git
synced 2024-11-25 23:49:11 +00:00
🎨 修改错误报告的形式为文件发送
This commit is contained in:
parent
b252768dfc
commit
a653a25562
@ -1,7 +1,9 @@
|
|||||||
import html
|
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
import aiofiles
|
||||||
from telegram import Update, ReplyKeyboardRemove
|
from telegram import Update, ReplyKeyboardRemove
|
||||||
from telegram.constants import ParseMode
|
from telegram.constants import ParseMode
|
||||||
from telegram.error import BadRequest, Forbidden
|
from telegram.error import BadRequest, Forbidden
|
||||||
@ -12,6 +14,13 @@ from core.plugin import error_handler, Plugin
|
|||||||
from utils.log import logger
|
from utils.log import logger
|
||||||
|
|
||||||
notice_chat_id = bot.config.error_notification_chat_id
|
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):
|
class ErrorHandler(Plugin):
|
||||||
@ -27,41 +36,38 @@ class ErrorHandler(Plugin):
|
|||||||
return
|
return
|
||||||
|
|
||||||
tb_list = traceback.format_exception(None, context.error, context.error.__traceback__)
|
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)
|
update_str = update.to_dict() if isinstance(update, Update) else str(update)
|
||||||
text_1 = (
|
|
||||||
f'<b>处理函数时发生异常</b> \n'
|
error_text = (
|
||||||
f'Exception while handling an update \n'
|
f"-----Exception while handling an update-----\n"
|
||||||
f'<pre>update = {html.escape(json.dumps(update_str, indent=2, ensure_ascii=False))}'
|
f"update = {json.dumps(update_str, indent=2, ensure_ascii=False)}\n"
|
||||||
'</pre>\n\n'
|
f"context.chat_data = {str(context.chat_data)}\n"
|
||||||
f'<pre>context.chat_data = {html.escape(str(context.chat_data))}</pre>\n\n'
|
f"context.user_data = {str(context.user_data)}\n"
|
||||||
f'<pre>context.user_data = {html.escape(str(context.user_data))}</pre>\n\n'
|
"\n"
|
||||||
)
|
"-----Traceback info-----\n"
|
||||||
text_2 = (
|
f"{tb_string}"
|
||||||
f'<pre>{html.escape(tb_string)}</pre>'
|
|
||||||
)
|
)
|
||||||
|
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:
|
try:
|
||||||
if 'make sure that only one bot instance is running' in tb_string:
|
if 'make sure that only one bot instance is running' in tb_string:
|
||||||
logger.error("其他机器人在运行,请停止!")
|
logger.error("其他机器人在运行,请停止!")
|
||||||
return
|
return
|
||||||
await context.bot.send_message(notice_chat_id, text_1, parse_mode=ParseMode.HTML)
|
await context.bot.send_document(chat_id=notice_chat_id, document=open(log_file, "rb"),
|
||||||
await context.bot.send_message(notice_chat_id, text_2, parse_mode=ParseMode.HTML)
|
caption="Error report.")
|
||||||
except BadRequest as exc:
|
except (BadRequest, Forbidden) as exc:
|
||||||
if 'too long' in str(exc):
|
logger.error("发送日记失败")
|
||||||
text = (
|
logger.exception(exc)
|
||||||
f'<b>处理函数时发生异常,traceback太长导致无法发送,但已写入日志</b> \n'
|
except FileNotFoundError:
|
||||||
f'<code>{html.escape(str(context.error))}</code>'
|
logger.error("发送日记失败 文件不存在")
|
||||||
)
|
|
||||||
try:
|
|
||||||
await context.bot.send_message(notice_chat_id, text, parse_mode=ParseMode.HTML)
|
|
||||||
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:
|
|
||||||
logger.exception(exc_1)
|
|
||||||
effective_user = update.effective_user
|
effective_user = update.effective_user
|
||||||
effective_message = update.effective_message
|
effective_message = update.effective_message
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user