From 96f9b9148953f30d90bffea50924e241ec16d3c9 Mon Sep 17 00:00:00 2001 From: coffee Date: Fri, 10 Apr 2026 11:39:04 +0800 Subject: [PATCH] fix(gateway): replace assertions with proper error handling in Telegram and Feishu MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python assertions are stripped when running with `python -O` (optimized mode), making them unsuitable for runtime error handling. 1. `telegram_network.py:113` — After exhausting all fallback IPs, the code uses `assert last_error is not None` before `raise last_error`. In optimized mode, the assert is skipped; if `last_error` is unexpectedly None, `raise None` produces a confusing `TypeError` instead of a meaningful error. Replace with an explicit `if` check that raises `RuntimeError` with a descriptive message. 2. `feishu.py:975` — The `_configure_with_overrides` closure uses `assert original_configure is not None` as a guard. While the outer scope only installs this closure when `original_configure` is not None, the assert would silently disappear in optimized mode. Replace with an explicit `if` check for defensive safety. --- gateway/platforms/feishu.py | 3 ++- gateway/platforms/telegram_network.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/gateway/platforms/feishu.py b/gateway/platforms/feishu.py index fad13bb0d..a53dbab0d 100644 --- a/gateway/platforms/feishu.py +++ b/gateway/platforms/feishu.py @@ -973,7 +973,8 @@ def _run_official_feishu_ws_client(ws_client: Any, adapter: Any) -> None: return await original_connect(*args, **kwargs) def _configure_with_overrides(conf: Any) -> Any: - assert original_configure is not None + if original_configure is None: + raise RuntimeError("Feishu _configure_with_overrides called but original_configure is None") result = original_configure(conf) _apply_runtime_ws_overrides() return result diff --git a/gateway/platforms/telegram_network.py b/gateway/platforms/telegram_network.py index 2b26ab916..d9832a269 100644 --- a/gateway/platforms/telegram_network.py +++ b/gateway/platforms/telegram_network.py @@ -110,7 +110,8 @@ class TelegramFallbackTransport(httpx.AsyncBaseTransport): logger.warning("[Telegram] Fallback IP %s failed: %s", ip, exc) continue - assert last_error is not None + if last_error is None: + raise RuntimeError("All Telegram fallback IPs exhausted but no error was recorded") raise last_error async def aclose(self) -> None: