2023-11-23 06:27:25 +00:00
|
|
|
import difflib
|
2022-09-11 13:15:01 +00:00
|
|
|
import os
|
|
|
|
import time
|
2022-07-26 10:07:31 +00:00
|
|
|
import traceback
|
2023-03-14 01:27:22 +00:00
|
|
|
from typing import Optional
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-09-11 13:15:01 +00:00
|
|
|
import aiofiles
|
2023-03-14 02:40:27 +00:00
|
|
|
from httpx import HTTPError, TimeoutException
|
2024-11-13 09:15:15 +00:00
|
|
|
from playwright.async_api import Error as PlaywrightError, TimeoutError as PlaywrightTimeoutError
|
2023-07-19 03:37:30 +00:00
|
|
|
from simnet.errors import (
|
|
|
|
DataNotPublic,
|
|
|
|
BadRequest as SIMNetBadRequest,
|
|
|
|
InvalidCookies,
|
|
|
|
TooManyRequests,
|
|
|
|
CookieException,
|
2023-07-19 12:28:19 +00:00
|
|
|
TimedOut as SIMNetTimedOut,
|
|
|
|
SIMNetException,
|
2023-11-16 05:19:02 +00:00
|
|
|
NeedChallenge,
|
2023-11-18 16:46:27 +00:00
|
|
|
InvalidDevice,
|
2023-07-19 03:37:30 +00:00
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
from telegram import ReplyKeyboardRemove, Update, InlineKeyboardMarkup, InlineKeyboardButton
|
2022-07-26 10:07:31 +00:00
|
|
|
from telegram.constants import ParseMode
|
2023-03-14 01:27:22 +00:00
|
|
|
from telegram.error import BadRequest, Forbidden, TelegramError, TimedOut, NetworkError
|
2023-03-14 05:53:05 +00:00
|
|
|
from telegram.ext import CallbackContext, ApplicationHandlerStop, filters
|
2023-03-14 01:27:22 +00:00
|
|
|
from telegram.helpers import create_deep_linked_url
|
2022-07-26 10:07:31 +00:00
|
|
|
|
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
|
2024-11-13 09:29:25 +00:00
|
|
|
from gram_core.services.cookies.error import CookieServiceError as TooManyRequestPublicCookies
|
2023-08-21 14:42:44 +00:00
|
|
|
from gram_core.services.players.error import PlayerNotFoundError
|
2023-03-14 01:27:22 +00:00
|
|
|
from modules.apihelper.error import APIHelperException, APIHelperTimedOut, ResponseException, ReturnCodeError
|
|
|
|
from modules.errorpush import (
|
|
|
|
PbClient,
|
|
|
|
PbClientException,
|
|
|
|
SentryClient,
|
|
|
|
SentryClientException,
|
|
|
|
)
|
2023-08-21 14:42:44 +00:00
|
|
|
from plugins.tools.genshin import CookiesNotFoundError, PlayerNotFoundError as GenshinPlayerNotFoundError
|
2022-09-08 01:08:37 +00:00
|
|
|
from utils.log import logger
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2023-01-07 08:00:32 +00:00
|
|
|
try:
|
|
|
|
import ujson as jsonlib
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
import json as jsonlib
|
|
|
|
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
class ErrorHandler(Plugin):
|
2023-03-14 01:27:22 +00:00
|
|
|
ERROR_MSG_PREFIX = "出错了呜呜呜 ~ "
|
|
|
|
SEND_MSG_ERROR_NOTICE = "发送 update_id[%s] 错误信息失败 错误信息为 [%s]"
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.notice_chat_id = 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)
|
|
|
|
self.report_dir = os.path.join(current_dir, "report")
|
|
|
|
if not os.path.exists(self.report_dir):
|
|
|
|
os.mkdir(self.report_dir)
|
|
|
|
self.pb_client = PbClient(config.error.pb_url, config.error.pb_sunset, config.error.pb_max_lines)
|
|
|
|
self.sentry = SentryClient(config.error.sentry_dsn)
|
2023-11-23 06:27:25 +00:00
|
|
|
self.tb_string = ""
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
|
|
async def notice_user(self, update: object, context: CallbackContext, content: str):
|
|
|
|
if not isinstance(update, Update):
|
|
|
|
logger.warning("错误的消息类型 %s", repr(update))
|
|
|
|
return None
|
|
|
|
if update.inline_query is not None: # 忽略 inline_query
|
|
|
|
return None
|
|
|
|
|
2024-03-10 12:40:26 +00:00
|
|
|
user = update.effective_user
|
|
|
|
message = update.effective_message
|
|
|
|
chat = update.effective_chat
|
|
|
|
|
2023-12-18 16:51:36 +00:00
|
|
|
_import_button = InlineKeyboardButton(
|
|
|
|
"从其他 BOT 导入", url=create_deep_linked_url(context.bot.username, "cookies_import")
|
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
if "重新绑定" in content:
|
|
|
|
buttons = InlineKeyboardMarkup(
|
2023-12-18 16:51:36 +00:00
|
|
|
[
|
|
|
|
[
|
|
|
|
InlineKeyboardButton(
|
|
|
|
"点我重新绑定", url=create_deep_linked_url(context.bot.username, "set_cookie")
|
|
|
|
),
|
|
|
|
_import_button,
|
|
|
|
],
|
|
|
|
]
|
2023-03-14 01:27:22 +00:00
|
|
|
)
|
|
|
|
elif "通过验证" in content:
|
|
|
|
buttons = InlineKeyboardMarkup(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
InlineKeyboardButton(
|
|
|
|
"点我通过验证", url=create_deep_linked_url(context.bot.username, "verify_verification")
|
|
|
|
)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
2023-08-21 14:42:44 +00:00
|
|
|
elif "绑定账号" in content:
|
|
|
|
buttons = InlineKeyboardMarkup(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
InlineKeyboardButton(
|
|
|
|
"点我绑定账号", url=create_deep_linked_url(self.application.bot.username, "set_cookie")
|
2023-12-18 16:51:36 +00:00
|
|
|
),
|
|
|
|
_import_button,
|
|
|
|
],
|
2023-08-21 14:42:44 +00:00
|
|
|
]
|
|
|
|
)
|
2024-03-10 12:40:26 +00:00
|
|
|
# recommend: channel alias
|
|
|
|
if message and message.sender_chat:
|
|
|
|
content += "\n\n推荐使用 /channel_alias 开启频道透视模式,派蒙将会把你当做普通用户运行命令。"
|
2023-03-14 01:27:22 +00:00
|
|
|
else:
|
|
|
|
buttons = ReplyKeyboardRemove()
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2024-06-19 08:29:47 +00:00
|
|
|
if (not chat) or chat.id == user.id:
|
2024-03-10 12:40:26 +00:00
|
|
|
logger.info("用户 %s[%s] 尝试通知错误信息[%s]", user.full_name, user.id, content)
|
2023-03-14 01:27:22 +00:00
|
|
|
else:
|
2024-03-10 12:40:26 +00:00
|
|
|
self.log_user(update, logger.info, "尝试通知在 %s[%s] 的错误信息[%s]", chat.title, chat.id, content)
|
2023-03-14 01:27:22 +00:00
|
|
|
try:
|
|
|
|
if update.callback_query:
|
|
|
|
await update.callback_query.answer(content, show_alert=True)
|
2023-04-11 01:44:39 +00:00
|
|
|
return
|
|
|
|
if message:
|
2024-06-10 16:58:39 +00:00
|
|
|
reply_text = await message.reply_text(content, reply_markup=buttons)
|
2023-03-17 04:06:58 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(reply_text):
|
|
|
|
self.add_delete_message_job(reply_text, context=context)
|
|
|
|
self.add_delete_message_job(message, context=context)
|
2023-04-11 01:44:39 +00:00
|
|
|
return
|
2023-03-14 01:27:22 +00:00
|
|
|
except TelegramError as exc:
|
|
|
|
logger.error(self.SEND_MSG_ERROR_NOTICE, update.update_id, exc.message)
|
|
|
|
except Exception as exc:
|
|
|
|
logger.error(self.SEND_MSG_ERROR_NOTICE, update.update_id, repr(exc), exc_info=exc)
|
|
|
|
|
|
|
|
def create_notice_task(self, update: object, context: CallbackContext, content: str):
|
|
|
|
context.application.create_task(self.notice_user(update, context, content), update)
|
|
|
|
|
|
|
|
@error_handler()
|
|
|
|
async def process_genshin_exception(self, update: object, context: CallbackContext):
|
2023-07-19 12:28:19 +00:00
|
|
|
if not isinstance(context.error, SIMNetException) or not isinstance(update, Update):
|
2022-11-24 15:02:27 +00:00
|
|
|
return
|
2023-03-14 01:27:22 +00:00
|
|
|
exc = context.error
|
|
|
|
notice: Optional[str] = None
|
2023-07-19 12:28:19 +00:00
|
|
|
if isinstance(exc, SIMNetTimedOut):
|
|
|
|
notice = self.ERROR_MSG_PREFIX + " 服务器熟啦 ~ 请稍后再试"
|
|
|
|
self.create_notice_task(update, context, notice)
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
if not isinstance(exc, SIMNetBadRequest) or not isinstance(update, Update):
|
|
|
|
return
|
2023-03-14 01:27:22 +00:00
|
|
|
if isinstance(exc, TooManyRequests):
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "Cookie 无效,请尝试重新绑定"
|
|
|
|
elif isinstance(exc, InvalidCookies):
|
|
|
|
if exc.retcode in (10001, -100):
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "Cookie 无效,请尝试重新绑定"
|
|
|
|
elif exc.retcode == 10103:
|
|
|
|
notice = (
|
|
|
|
self.ERROR_MSG_PREFIX
|
2024-07-04 11:09:48 +00:00
|
|
|
+ "Cookie 有效,但没有绑定到游戏帐户,请尝试登录通行证,在账号管理里面选择账号游戏信息,将绝区零设置为默认角色。"
|
2024-03-16 01:44:15 +00:00
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
else:
|
|
|
|
logger.error("未知Cookie错误", exc_info=exc)
|
|
|
|
notice = self.ERROR_MSG_PREFIX + f"Cookie 无效 错误信息为 {exc.original} 请尝试重新绑定"
|
2023-07-19 03:37:30 +00:00
|
|
|
elif isinstance(exc, CookieException):
|
|
|
|
if exc.retcode == 0:
|
2023-09-06 07:27:03 +00:00
|
|
|
notice = self.ERROR_MSG_PREFIX + "Cookie 已经被刷新,请尝试重试发送命令~"
|
2023-07-19 03:37:30 +00:00
|
|
|
else:
|
|
|
|
logger.error("未知Cookie错误", exc_info=exc)
|
|
|
|
notice = self.ERROR_MSG_PREFIX + f"Cookie 无效 错误信息为 {exc.original} 请尝试重新绑定"
|
2023-11-18 16:46:27 +00:00
|
|
|
elif isinstance(exc, InvalidDevice):
|
2023-12-17 10:38:58 +00:00
|
|
|
notice = self.ERROR_MSG_PREFIX + "设备信息无效,请尝试重新绑定"
|
2023-03-14 01:27:22 +00:00
|
|
|
elif isinstance(exc, DataNotPublic):
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "查询的用户数据未公开"
|
2023-11-16 05:19:02 +00:00
|
|
|
elif isinstance(exc, NeedChallenge):
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "服务器检测到该账号可能存在异常,请求被拒绝,请尝试通过验证"
|
2023-03-14 01:27:22 +00:00
|
|
|
else:
|
|
|
|
if exc.retcode == -130:
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "未设置默认角色,请尝试重新绑定"
|
|
|
|
elif exc.retcode == -500001:
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "网络出小差了,请稍后重试~"
|
|
|
|
elif exc.retcode == -1:
|
2023-07-19 02:55:52 +00:00
|
|
|
logger.warning("内部数据库错误 [%s]%s", exc.ret_code, exc.original)
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "系统内部数据库错误,请稍后重试~"
|
2023-03-14 01:27:22 +00:00
|
|
|
elif exc.retcode == -10001: # 参数异常 不应该抛出异常 进入下一步处理
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
logger.error("GenshinException", exc_info=exc)
|
2023-07-19 02:55:52 +00:00
|
|
|
message = exc.original if exc.original else exc.message
|
|
|
|
if message:
|
|
|
|
notice = self.ERROR_MSG_PREFIX + f"获取信息发生错误 错误信息为 {message} ~ 请稍后再试"
|
|
|
|
else:
|
2023-09-06 07:27:03 +00:00
|
|
|
notice = self.ERROR_MSG_PREFIX + "获取信息发生错误 请稍后再试"
|
2023-03-14 01:27:22 +00:00
|
|
|
if notice:
|
|
|
|
self.create_notice_task(update, context, notice)
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
|
|
|
@error_handler()
|
|
|
|
async def process_telegram_exception(self, update: object, context: CallbackContext):
|
|
|
|
if not isinstance(context.error, TelegramError) or not isinstance(update, Update):
|
|
|
|
return
|
|
|
|
notice: Optional[str] = None
|
2022-11-25 08:41:50 +00:00
|
|
|
if isinstance(context.error, TimedOut):
|
2023-03-16 15:18:39 +00:00
|
|
|
# notice = self.ERROR_MSG_PREFIX + " 连接 telegram 服务器超时"
|
|
|
|
logger.error("连接 telegram 服务器超时 [%s]", repr(context.error))
|
2023-03-17 00:31:31 +00:00
|
|
|
raise ApplicationHandlerStop
|
2023-03-27 02:55:12 +00:00
|
|
|
if isinstance(context.error, BadRequest):
|
2023-03-14 01:27:22 +00:00
|
|
|
if "Replied message not found" in context.error.message:
|
|
|
|
notice = "气死我了!怎么有人喜欢发一个命令就秒删了!"
|
|
|
|
elif "Message is not modified" in context.error.message:
|
|
|
|
logger.warning("编辑消息异常")
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
elif "Not enough rights" in context.error.message:
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "权限不足,请检查对应权限是否开启"
|
2023-03-15 08:20:01 +00:00
|
|
|
elif "Wrong file identifier specified" in context.error.message:
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "文件标识符未找到 ~ 请稍后再试"
|
2023-03-14 01:27:22 +00:00
|
|
|
else:
|
|
|
|
logger.error("python-telegram-bot 请求错误", exc_info=context.error)
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "telegram-bot-api请求错误 ~ 请稍后再试"
|
|
|
|
elif isinstance(context.error, Forbidden):
|
|
|
|
logger.error("python-telegram-bot 返回 Forbidden")
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "telegram-bot-api请求错误 ~ 请稍后再试"
|
|
|
|
if notice:
|
|
|
|
self.create_notice_task(update, context, notice)
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
|
|
|
@error_handler()
|
|
|
|
async def process_telegram_update_exception(self, update: object, context: CallbackContext):
|
|
|
|
if update is None and isinstance(context.error, NetworkError):
|
|
|
|
logger.error("python-telegram-bot NetworkError : %s", context.error.message)
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
|
|
|
@error_handler()
|
|
|
|
async def process_apihelper_exception(self, update: object, context: CallbackContext):
|
|
|
|
if not isinstance(context.error, APIHelperException) or not isinstance(update, Update):
|
|
|
|
return
|
|
|
|
exc = context.error
|
|
|
|
notice: Optional[str] = None
|
|
|
|
if isinstance(exc, APIHelperTimedOut):
|
2023-03-16 15:18:39 +00:00
|
|
|
notice = self.ERROR_MSG_PREFIX + " 服务器熟啦 ~ 请稍后再试"
|
2023-03-14 01:27:22 +00:00
|
|
|
elif isinstance(exc, ReturnCodeError):
|
|
|
|
notice = (
|
|
|
|
self.ERROR_MSG_PREFIX
|
|
|
|
+ f"API请求错误 错误信息为 {exc.message if exc.message else exc.code} ~ 请稍后再试"
|
2024-03-16 01:44:15 +00:00
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
elif isinstance(exc, ResponseException):
|
|
|
|
notice = (
|
|
|
|
self.ERROR_MSG_PREFIX
|
|
|
|
+ f"API请求错误 错误信息为 {exc.message if exc.message else exc.code} ~ 请稍后再试"
|
2024-03-16 01:44:15 +00:00
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
if notice:
|
|
|
|
self.create_notice_task(update, context, notice)
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
|
|
|
@error_handler()
|
|
|
|
async def process_httpx_exception(self, update: object, context: CallbackContext):
|
|
|
|
if not isinstance(context.error, HTTPError) or not isinstance(update, Update):
|
|
|
|
return
|
|
|
|
exc = context.error
|
|
|
|
notice: Optional[str] = None
|
2023-03-14 02:40:27 +00:00
|
|
|
if isinstance(exc, TimeoutException):
|
2023-03-16 15:18:39 +00:00
|
|
|
notice = self.ERROR_MSG_PREFIX + " 服务器熟啦 ~ 请稍后再试"
|
2023-08-21 04:06:27 +00:00
|
|
|
logger.warning("Httpx [%s]\n%s[%s]", exc.__class__.__name__, exc.request.method, exc.request.url)
|
2023-03-14 01:27:22 +00:00
|
|
|
if notice:
|
|
|
|
self.create_notice_task(update, context, notice)
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
2023-08-21 14:42:44 +00:00
|
|
|
@error_handler()
|
|
|
|
async def process_player_and_cookie_not_found(self, update: object, context: CallbackContext):
|
|
|
|
if not isinstance(
|
|
|
|
context.error, (CookiesNotFoundError, PlayerNotFoundError, GenshinPlayerNotFoundError)
|
|
|
|
) or not isinstance(update, Update):
|
|
|
|
return
|
2024-06-07 15:22:30 +00:00
|
|
|
self.create_notice_task(update, context, config.notice.user_not_found)
|
2023-08-21 14:42:44 +00:00
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
2024-07-04 11:09:48 +00:00
|
|
|
@error_handler()
|
|
|
|
async def process_public_cookies(self, update: object, context: CallbackContext):
|
2024-06-09 13:30:44 +00:00
|
|
|
if not isinstance(context.error, TooManyRequestPublicCookies) or not isinstance(update, Update):
|
2024-07-04 11:09:48 +00:00
|
|
|
return
|
2024-06-09 13:30:44 +00:00
|
|
|
self.create_notice_task(update, context, config.notice.user_not_found)
|
2024-07-04 11:09:48 +00:00
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
2024-11-13 09:15:15 +00:00
|
|
|
@error_handler()
|
|
|
|
async def process_playwright_exception(self, update: object, context: CallbackContext):
|
|
|
|
if not isinstance(context.error, PlaywrightError) or not isinstance(update, Update):
|
|
|
|
return
|
|
|
|
if isinstance(context.error, PlaywrightTimeoutError):
|
|
|
|
notice = self.ERROR_MSG_PREFIX + " 渲染超时 ~ 请稍后再试"
|
|
|
|
self.create_notice_task(update, context, notice)
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
@error_handler(block=False)
|
|
|
|
async def process_z_error(self, update: object, context: CallbackContext) -> None:
|
|
|
|
# 必须 `process_` 加上 `z` 保证该函数最后一个注册
|
|
|
|
"""记录错误并发送消息通知开发人员。
|
|
|
|
logger the error and send a telegram message to notify the developer."""
|
2023-11-23 06:27:25 +00:00
|
|
|
if isinstance(update, Update):
|
|
|
|
effective_user = update.effective_user
|
|
|
|
effective_message = update.effective_message
|
|
|
|
try:
|
|
|
|
if effective_message is not None:
|
|
|
|
chat = effective_message.chat
|
|
|
|
logger.info(
|
|
|
|
"尝试通知用户 %s[%s] 在 %s[%s] 的 update_id[%s] 错误信息",
|
|
|
|
effective_user.full_name,
|
|
|
|
effective_user.id,
|
|
|
|
chat.full_name,
|
|
|
|
chat.id,
|
|
|
|
update.update_id,
|
|
|
|
)
|
2024-06-07 15:22:30 +00:00
|
|
|
text = f"出错了呜呜呜 ~ {config.notice.bot_name}这边发生了点问题无法处理!"
|
2023-11-23 06:27:25 +00:00
|
|
|
await context.bot.send_message(
|
|
|
|
effective_message.chat_id, text, reply_markup=ReplyKeyboardRemove(), parse_mode=ParseMode.HTML
|
|
|
|
)
|
|
|
|
except NetworkError as exc:
|
|
|
|
logger.error("发送 update_id[%s] 错误信息失败 错误信息为 %s", update.update_id, exc.message)
|
|
|
|
|
|
|
|
tb_list = traceback.format_exception(None, context.error, context.error.__traceback__)
|
|
|
|
tb_string = "".join(tb_list)
|
2022-11-24 15:02:27 +00:00
|
|
|
|
2023-11-23 06:27:25 +00:00
|
|
|
if difflib.SequenceMatcher(None, self.tb_string, tb_string).quick_ratio() >= 0.93:
|
|
|
|
logger.debug("相似的错误信息已经发送过了")
|
|
|
|
return
|
|
|
|
self.tb_string = tb_string
|
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
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
if not self.notice_chat_id:
|
2022-07-26 10:07:31 +00:00
|
|
|
return
|
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"
|
2023-01-07 08:00:32 +00:00
|
|
|
f"update = {jsonlib.dumps(update_str, indent=2, ensure_ascii=False)}\n"
|
2022-09-11 13:15:01 +00:00
|
|
|
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"
|
2023-03-14 01:27:22 +00:00
|
|
|
log_file = os.path.join(self.report_dir, file_name)
|
2022-09-11 13:15:01 +00:00
|
|
|
try:
|
|
|
|
async with aiofiles.open(log_file, mode="w+", encoding="utf-8") as f:
|
|
|
|
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:
|
|
|
|
if "make sure that only one bot instance is running" in tb_string:
|
|
|
|
logger.error("其他机器人在运行,请停止!")
|
|
|
|
return
|
2022-09-11 13:15:01 +00:00
|
|
|
await context.bot.send_document(
|
2023-03-14 01:27:22 +00:00
|
|
|
chat_id=self.notice_chat_id,
|
2022-09-11 13:15:01 +00:00
|
|
|
document=open(log_file, "rb"),
|
2022-09-14 14:23:07 +00:00
|
|
|
caption=f'Error: "{context.error.__class__.__name__}"',
|
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
except NetworkError as exc:
|
2022-09-11 13:15:01 +00:00
|
|
|
logger.error("发送日记失败")
|
|
|
|
logger.exception(exc)
|
|
|
|
except FileNotFoundError:
|
|
|
|
logger.error("发送日记失败 文件不存在")
|
2023-03-14 01:27:22 +00:00
|
|
|
if self.pb_client.enabled:
|
2022-11-21 06:57:12 +00:00
|
|
|
logger.info("正在上传日记到 pb")
|
|
|
|
try:
|
2023-03-14 01:27:22 +00:00
|
|
|
pb_url = await self.pb_client.create_pb(error_text)
|
2022-11-21 06:57:12 +00:00
|
|
|
if pb_url:
|
|
|
|
logger.success("上传日记到 pb 成功")
|
|
|
|
await context.bot.send_message(
|
2023-03-14 01:27:22 +00:00
|
|
|
chat_id=self.notice_chat_id,
|
2022-11-21 06:57:12 +00:00
|
|
|
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)
|
2023-03-14 01:27:22 +00:00
|
|
|
if self.sentry.enabled:
|
2022-11-21 06:57:12 +00:00
|
|
|
logger.info("正在上传日记到 sentry")
|
|
|
|
try:
|
2023-03-14 01:27:22 +00:00
|
|
|
self.sentry.report_error(update, (type(context.error), context.error, context.error.__traceback__))
|
2022-11-21 06:57:12 +00:00
|
|
|
logger.success("上传日记到 sentry 成功")
|
|
|
|
except SentryClientException as exc:
|
|
|
|
logger.warning("上传错误信息至 sentry 失败", exc_info=exc)
|
|
|
|
except Exception as exc:
|
|
|
|
logger.error("上传错误信息至 sentry 失败")
|
|
|
|
logger.exception(exc)
|