🎨 优化错误平台模块

This commit is contained in:
洛水居室 2022-10-20 22:21:26 +08:00
parent c2b78b84fa
commit 57f0029a42
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
3 changed files with 22 additions and 9 deletions

View File

@ -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"]

View File

@ -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 成功")

View File

@ -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)