2022-09-08 01:08:37 +00:00
|
|
|
import json
|
2022-09-11 13:15:01 +00:00
|
|
|
import os
|
|
|
|
import time
|
2022-07-26 10:07:31 +00:00
|
|
|
import traceback
|
|
|
|
|
2022-09-11 13:15:01 +00:00
|
|
|
import aiofiles
|
2022-10-28 07:11:14 +00:00
|
|
|
from telegram import ReplyKeyboardRemove, Update
|
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
|
2022-11-21 06:57:12 +00:00
|
|
|
from core.config import config
|
2022-10-28 07:11:14 +00:00
|
|
|
from core.plugin import Plugin, error_handler
|
2022-11-21 06:57:12 +00:00
|
|
|
from modules.errorpush import PbClient, SentryClient, PbClientException, SentryClientException
|
2022-09-08 01:08:37 +00:00
|
|
|
from utils.log import logger
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-10-28 07:11:14 +00:00
|
|
|
notice_chat_id = bot.config.error.notification_chat_id
|
2022-09-11 13:15:01 +00:00
|
|
|
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)
|
2022-11-21 06:57:12 +00:00
|
|
|
pb_client = PbClient(config.error.pb_url, config.error.pb_sunset, config.error.pb_max_lines)
|
|
|
|
sentry = SentryClient(config.error.sentry_dsn)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
class ErrorHandler(Plugin):
|
|
|
|
@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-10-21 12:31:58 +00:00
|
|
|
if not notice_chat_id:
|
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__)
|
2022-09-11 13:15:01 +00:00
|
|
|
tb_string = "".join(tb_list)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
|
|
|
update_str = update.to_dict() if isinstance(update, Update) else str(update)
|
2022-09-11 13:15:01 +00:00
|
|
|
|
|
|
|
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}"
|
2022-09-08 01:08:37 +00:00
|
|
|
)
|
2022-09-11 13:15:01 +00:00
|
|
|
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:
|
2022-10-10 11:07:28 +00:00
|
|
|
async with aiofiles.open(log_file, mode="w+", encoding="utf-8") as f:
|
2022-09-11 13:15:01 +00:00
|
|
|
await f.write(error_text)
|
2022-10-11 06:45:07 +00:00
|
|
|
except Exception as exc: # pylint: disable=W0703
|
2022-09-11 13:15:01 +00:00
|
|
|
logger.error("保存日记失败")
|
|
|
|
logger.exception(exc)
|
2022-09-08 01:08:37 +00:00
|
|
|
try:
|
2022-10-10 11:07:28 +00:00
|
|
|
if "make sure that only one bot instance is running" in tb_string:
|
2022-09-08 01:08:37 +00:00
|
|
|
logger.error("其他机器人在运行,请停止!")
|
|
|
|
return
|
2022-10-10 11:07:28 +00:00
|
|
|
await context.bot.send_document(
|
|
|
|
chat_id=notice_chat_id,
|
|
|
|
document=open(log_file, "rb"),
|
|
|
|
caption=f'Error: "{context.error.__class__.__name__}"',
|
|
|
|
)
|
2022-09-11 13:15:01 +00:00
|
|
|
except (BadRequest, Forbidden) as exc:
|
|
|
|
logger.error("发送日记失败")
|
|
|
|
logger.exception(exc)
|
|
|
|
except FileNotFoundError:
|
|
|
|
logger.error("发送日记失败 文件不存在")
|
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
|
2022-10-10 11:07:28 +00:00
|
|
|
logger.info(
|
|
|
|
f"尝试通知用户 {effective_user.full_name}[{effective_user.id}] "
|
|
|
|
f"在 {chat.full_name}[{chat.id}]"
|
|
|
|
f"的 update_id[{update.update_id}] 错误信息"
|
|
|
|
)
|
2022-09-08 01:08:37 +00:00
|
|
|
text = "出错了呜呜呜 ~ 派蒙这边发生了点问题无法处理!"
|
2022-10-10 11:07:28 +00:00
|
|
|
await context.bot.send_message(
|
|
|
|
effective_message.chat_id, text, reply_markup=ReplyKeyboardRemove(), parse_mode=ParseMode.HTML
|
|
|
|
)
|
2022-09-08 01:08:37 +00:00
|
|
|
except (BadRequest, Forbidden) as exc:
|
|
|
|
logger.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为")
|
|
|
|
logger.exception(exc)
|
2022-11-21 06:57:12 +00:00
|
|
|
if pb_client.enabled:
|
|
|
|
logger.info("正在上传日记到 pb")
|
|
|
|
try:
|
|
|
|
pb_url = await pb_client.create_pb(error_text)
|
|
|
|
if pb_url:
|
|
|
|
logger.success("上传日记到 pb 成功")
|
|
|
|
await context.bot.send_message(
|
|
|
|
chat_id=notice_chat_id,
|
|
|
|
text=f"错误信息已上传至 <a href='{pb_url}'>fars</a> 请查看",
|
|
|
|
parse_mode=ParseMode.HTML,
|
|
|
|
)
|
|
|
|
except PbClientException as exc:
|
|
|
|
logger.warning("上传错误信息至 fars 失败", exc_info=exc)
|
|
|
|
except Exception as exc:
|
|
|
|
logger.error("上传错误信息至 fars 失败")
|
|
|
|
logger.exception(exc)
|
|
|
|
if sentry.enabled:
|
|
|
|
logger.info("正在上传日记到 sentry")
|
|
|
|
try:
|
|
|
|
sentry.report_error(update, (type(context.error), context.error, context.error.__traceback__))
|
|
|
|
logger.success("上传日记到 sentry 成功")
|
|
|
|
except SentryClientException as exc:
|
|
|
|
logger.warning("上传错误信息至 sentry 失败", exc_info=exc)
|
|
|
|
except Exception as exc:
|
|
|
|
logger.error("上传错误信息至 sentry 失败")
|
|
|
|
logger.exception(exc)
|