From 60b8e46924de1308e044cf6d8de4f48297dc7922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Mon, 21 Nov 2022 12:23:22 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Improve=20error=20handling=20in?= =?UTF-8?q?=20`create=5Fpb`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/error/pb.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/modules/error/pb.py b/modules/error/pb.py index 266dc67..f2f22b6 100644 --- a/modules/error/pb.py +++ b/modules/error/pb.py @@ -1,3 +1,5 @@ +from typing import Optional + import httpx from core.config import config @@ -12,11 +14,11 @@ class PbClient: self.private: bool = True self.max_lines: int = config.error.pb_max_lines - async def create_pb(self, content: str) -> str: + async def create_pb(self, content: str) -> Optional[str]: if not self.PB_API: - return "" + return None logger.info("正在上传日记到 pb") - content = "\n".join(content.splitlines()[-self.max_lines :]) + "\n" + content = "\n".join(content.splitlines()[-self.max_lines:]) + "\n" data = { "c": content, } @@ -24,6 +26,9 @@ class PbClient: data["p"] = "1" if self.sunset: data["sunset"] = self.sunset - data = await self.client.post(self.PB_API, data=data) # 需要错误处理 + result = await self.client.post(self.PB_API, data=data) + if result.is_error: + logger.warning("上传日记到 pb 失败 status_code[%s]", result.status_code) + return None logger.success("上传日记到 pb 成功") - return data.headers["location"] + return result.headers.get("location")