diff --git a/modules/error/pb.py b/modules/error/pb.py index 683f63f..05592e4 100644 --- a/modules/error/pb.py +++ b/modules/error/pb.py @@ -1,20 +1,22 @@ import httpx from core.config import config +from utils.log import logger class PbClient: def __init__(self): self.client = httpx.AsyncClient() self.PB_API = config.error_pb_url - self.sunset: int = config.error_pb_sunset # 自动销毁时间 秒 + self.sunset: int = config.error_pb_sunset # 自动销毁时间 单位为秒 self.private: bool = True self.max_lines: int = config.error_pb_max_lines async def create_pb(self, content: str) -> str: if not self.PB_API: return "" - content = "\n".join(content.splitlines()[-self.max_lines :]) + "\n" + logger.info("正在上传日记到 pb") + content = "\n".join(content.splitlines()[-self.max_lines:]) + "\n" data = { "c": content, } @@ -23,4 +25,5 @@ class PbClient: if self.sunset: data["sunset"] = self.sunset data = await self.client.post(self.PB_API, data=data) # 需要错误处理 + logger.success("上传日记到 pb 成功") return data.headers["location"] diff --git a/modules/error/sentry.py b/modules/error/sentry.py index 1c0c967..3f132a3 100644 --- a/modules/error/sentry.py +++ b/modules/error/sentry.py @@ -10,6 +10,7 @@ from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration from telegram import Update from core.config import config +from utils.log import logger repo = Repo(os.getcwd()) sentry_sdk_git_hash = rev_parse(repo, "HEAD").hexsha @@ -29,14 +30,23 @@ sentry_sdk.init( class Sentry: @staticmethod - def report_error(update: Update, exc_info): + def report_error(update: object, exc_info): if not config.error_sentry_dsn: return - try: - sender_id = update.effective_user.id if update.effective_user else update.effective_chat.id - except AttributeError: - sender_id = 0 + logger.info("正在上传日记到 sentry") + message: str = "" + chat_id: int = 0 + user_id: int = 0 + if isinstance(update, Update): + if update.effective_user: + chat_id = update.effective_user.id + if update.effective_chat: + user_id = update.effective_chat.id + if update.effective_message: + if update.effective_message.text: + message = update.effective_message.text sentry_sdk.set_context( - "Target", {"ChatID": str(update.message.chat_id), "UserID": sender_id, "Msg": update.message.text or ""} + "Target", {"ChatID": str(chat_id), "UserID": str(user_id), "Msg": message} ) sentry_sdk.capture_exception(exc_info) + logger.success("上传日记到 sentry 成功") diff --git a/plugins/system/errorhandler.py b/plugins/system/errorhandler.py index ad82ef5..ca5a92a 100644 --- a/plugins/system/errorhandler.py +++ b/plugins/system/errorhandler.py @@ -106,7 +106,7 @@ class ErrorHandler(Plugin): logger.error("上传错误信息至 fars 失败") logger.exception(exc) try: - sentry.report_error(update, context.error) + sentry.report_error(update, (type(context.error), context.error, context.error.__traceback__)) except Exception as exc: # pylint: disable=W0703 logger.error("上传错误信息至 sentry 失败") logger.exception(exc)