From 4c10475dcaf07cd96e2c002ed0331d45a0aa891b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Thu, 17 Nov 2022 20:36:25 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Update=20Exception-Handling=20fo?= =?UTF-8?q?r=20`send=5Fuser=5Fnotification`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/decorators/error.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/utils/decorators/error.py b/utils/decorators/error.py index 62d7ba34..a8599522 100644 --- a/utils/decorators/error.py +++ b/utils/decorators/error.py @@ -32,16 +32,20 @@ async def send_user_notification(update: Update, context: CallbackContext, text: chat = update.effective_chat if message is None: update_str = update.to_dict() if isinstance(update, Update) else str(update) - logger.warning("错误的消息类型\n" + json.dumps(update_str, indent=2, ensure_ascii=False)) + logger.warning("错误的消息类型\n %s", json.dumps(update_str, indent=2, ensure_ascii=False)) return - logger.info(f"尝试通知用户 {user.full_name}[{user.id}] " f"在 {chat.full_name}[{chat.id}]" f"的 错误信息[{text}]") + logger.info("尝试通知用户 %s[%s] 在 %s[%s] 的错误信息[%s]", user.full_name, user.id, chat.full_name, chat.id, text) try: await message.reply_text(text, reply_markup=buttons, allow_sending_without_reply=True) - except (BadRequest, Forbidden, Exception) as exc: - logger.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为") + except ConnectTimeout: + logger.error("httpx 模块连接服务器 ConnectTimeout 发送 update_id[%s] 错误信息失败", update.update_id) + except BadRequest as exc: + logger.error("发送 update_id[%s] 错误信息失败 错误信息为 [%s]", update.update_id, exc.message) + except Forbidden as exc: + logger.error("发送 update_id[%s] 错误信息失败 错误信息为 [%s]", update.update_id, exc.message) + except Exception as exc: + logger.error("发送 update_id[%s] 错误信息失败 错误信息为 [%s]", update.update_id, repr(exc)) logger.exception(exc) - finally: - pass def telegram_warning(update: Update, text: str):