Skip to content

The agent that grows with you

fix(gateway): preserve exception type when error string is empty (#78183)

Tranquil-Flow6cbb7b611581
diff --git a/gateway/platforms/bluebubbles.py b/gateway/platforms/bluebubbles.py
index dd226ad7523..23e1621f3b9 100644
--- a/gateway/platforms/bluebubbles.py
+++ b/gateway/platforms/bluebubbles.py
@@ -519,7 +519,7 @@ class BlueBubblesAdapter(BasePlatformAdapter):
msg_id = data.get("guid") or data.get("messageGuid") or "ok"
return SendResult(success=True, message_id=str(msg_id), raw_response=res)
except Exception as exc:
- return SendResult(success=False, error=str(exc))
+ return SendResult(success=False, error=str(exc) or type(exc).__name__)
# ------------------------------------------------------------------
# Text sending
@@ -582,7 +582,7 @@ class BlueBubblesAdapter(BasePlatformAdapter):
success=True, message_id=str(msg_id), raw_response=res
)
except Exception as exc:
- return SendResult(success=False, error=str(exc))
+ return SendResult(success=False, error=str(exc) or type(exc).__name__)
return last
# ------------------------------------------------------------------
diff --git a/gateway/platforms/qqbot/adapter.py b/gateway/platforms/qqbot/adapter.py
index d540692c095..e36eacd2e0d 100644
--- a/gateway/platforms/qqbot/adapter.py
+++ b/gateway/platforms/qqbot/adapter.py
@@ -2545,7 +2545,7 @@ class QQAdapter(BasePlatformAdapter):
)
await asyncio.sleep(delay)
- error_msg = str(last_exc) if last_exc else "Unknown error"
+ error_msg = (str(last_exc) or type(last_exc).__name__) if last_exc else "Unknown error"
logger.error("[%s] Send failed: %s", self._log_tag, error_msg)
retryable = not any(
k in error_msg.lower() for k in ("invalid", "forbidden", "not found")
@@ -2661,7 +2661,7 @@ class QQAdapter(BasePlatformAdapter):
logger.error(
"[%s] send_with_keyboard failed: %s", self._log_tag, exc
)
- return SendResult(success=False, error=str(exc))
+ return SendResult(success=False, error=str(exc) or type(exc).__name__)
async def send_approval_request(
self,
@@ -3013,7 +3013,7 @@ class QQAdapter(BasePlatformAdapter):
)
except Exception as exc:
logger.error("[%s] Media send failed: %s", self._log_tag, exc)
- return SendResult(success=False, error=str(exc))
+ return SendResult(success=False, error=str(exc) or type(exc).__name__)
async def _upload_local_file(
self,
diff --git a/gateway/platforms/whatsapp_cloud.py b/gateway/platforms/whatsapp_cloud.py
index baec4a5b69c..bf371f0495d 100644
--- a/gateway/platforms/whatsapp_cloud.py
+++ b/gateway/platforms/whatsapp_cloud.py
@@ -552,7 +552,7 @@ class WhatsAppCloudAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):
resp = await self._http_client.post(url, headers=headers, json=payload)
except Exception as exc:
logger.exception("[whatsapp_cloud] send failed")
- return SendResult(success=False, error=str(exc))
+ return SendResult(success=False, error=str(exc) or type(exc).__name__)
if resp.status_code != 200:
# Meta returns structured errors in the body — surface them
@@ -710,7 +710,7 @@ class WhatsAppCloudAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):
resp = await self._http_client.post(url, headers=headers, json=payload)
except Exception as exc:
logger.exception("[whatsapp_cloud] interactive send failed")
- return SendResult(success=False, error=str(exc))
+ return SendResult(success=False, error=str(exc) or type(exc).__name__)
if resp.status_code != 200:
try:
@@ -1086,7 +1086,7 @@ class WhatsAppCloudAdapter(WhatsAppBehaviorMixin, BasePlatformAdapter):
resp = await self._http_client.post(url, headers=headers, json=payload)
except Exception as exc:
logger.exception("[whatsapp_cloud] media send failed")
- return SendResult(success=False, error=str(exc))
+ return SendResult(success=False, error=str(exc) or type(exc).__name__)
if resp.status_code != 200:
try:
diff --git a/gateway/platforms/yuanbao.py b/gateway/platforms/yuanbao.py
index 7dc3f41b843..46254bf0128 100644
--- a/gateway/platforms/yuanbao.py
+++ b/gateway/platforms/yuanbao.py
@@ -3897,7 +3897,7 @@ class MediaSendHandler(ABC):
"[%s] %s.handle() failed: %s",
adapter.name, handler_name, exc, exc_info=True,
)
- return SendResult(success=False, error=str(exc))
+ return SendResult(success=False, error=str(exc) or type(exc).__name__)
class ImageUrlHandler(MediaSendHandler):
diff --git a/tests/gateway/test_bluebubbles.py b/tests/gateway/test_bluebubbles.py
index 994a2e0c809..695a26297d8 100644
--- a/tests/gateway/test_bluebubbles.py
+++ b/tests/gateway/test_bluebubbles.py
@@ -2,9 +2,11 @@
import asyncio
import json
+import httpx
import pytest
from gateway.config import Platform, PlatformConfig
+from gateway.platforms.base import BasePlatformAdapter
def _make_adapter(monkeypatch, **extra):
@@ -479,3 +481,89 @@ class TestBlueBubblesWebhookRegistration:
assert len(deleted_ids) == 2
+# ---------------------------------------------------------------------------
+# Regression for #78183: httpx timeout exceptions stringify to "" which
+# defeats _is_timeout_error, causing the plain-text fallback to re-send an
+# already-delivered message (duplicate delivery).
+# ---------------------------------------------------------------------------
+
+class TestBlueBubblesTimeoutErrorNormalization:
+ """When an httpx timeout has an empty string representation, the adapter
+ must fall back to the exception type name so the base-layer timeout guard
+ can still recognise it."""
+
+ @pytest.mark.asyncio
+ async def test_send_read_timeout_produces_matchable_error(self, monkeypatch):
+ adapter = _make_adapter(monkeypatch)
+
+ async def fake_resolve(chat_id):
+ return "iMessage;+;chat-123"
+ monkeypatch.setattr(adapter, "_resolve_chat_guid", fake_resolve)
+
+ async def fake_api_post(path, payload):
+ raise httpx.ReadTimeout("")
+ monkeypatch.setattr(adapter, "_api_post", fake_api_post)
+
+ result = await adapter.send("chat-1", "hello world")
+
+ assert not result.success
+ assert result.error, "error must not be empty"
+ assert BasePlatformAdapter._is_timeout_error(result.error), (
+ f"_is_timeout_error must recognise {result.error!r}"
+ )
+
+ @pytest.mark.asyncio
+ async def test_send_write_timeout_produces_matchable_error(self, monkeypatch):
+ adapter = _make_adapter(monkeypatch)
+
+ async def fake_resolve(chat_id):
+ return "iMessage;+;chat-123"
+ monkeypatch.setattr(adapter, "_resolve_chat_guid", fake_resolve)
+
+ async def fake_api_post(path, payload):
+ raise httpx.WriteTimeout("")
+ monkeypatch.setattr(adapter, "_api_post", fake_api_post)
+
+ result = await adapter.send("chat-1", "hello world")
+
+ assert not result.success
+ assert result.error
+ assert BasePlatformAdapter._is_timeout_error(result.error)
+
+ @pytest.mark.asyncio
+ async def test_create_chat_for_handle_timeout_produces_matchable_error(
+ self, monkeypatch,
+ ):
+ """Sibling call path — _create_chat_for_handle has the same
+ error=str(exc) pattern and must also preserve the exception type."""
+ adapter = _make_adapter(monkeypatch)
+
+ async def fake_api_post(path, payload):
+ raise httpx.ReadTimeout("")
+ monkeypatch.setattr(adapter, "_api_post", fake_api_post)
+
+ result = await adapter._create_chat_for_handle("[email protected]", "hi")
+
+ assert not result.success
+ assert result.error
+ assert BasePlatformAdapter._is_timeout_error(result.error)
+
+ @pytest.mark.asyncio
+ async def test_non_empty_error_string_is_unchanged(self, monkeypatch):
+ """A normal exception with a message must keep its original text."""
+ adapter = _make_adapter(monkeypatch)
+
+ async def fake_resolve(chat_id):
+ return "iMessage;+;chat-123"
+ monkeypatch.setattr(adapter, "_resolve_chat_guid", fake_resolve)
+
+ async def fake_api_post(path, payload):
+ raise RuntimeError("Server error '500 Internal Server Error'")
+ monkeypatch.setattr(adapter, "_api_post", fake_api_post)
+
+ result = await adapter.send("chat-1", "hello world")
+
+ assert not result.success
+ assert "500 Internal Server Error" in (result.error or "")
+
+
diff --git a/tests/gateway/test_qqbot.py b/tests/gateway/test_qqbot.py
index a83d05bb978..7ea3ceb1433 100644
--- a/tests/gateway/test_qqbot.py
+++ b/tests/gateway/test_qqbot.py
@@ -439,6 +439,55 @@ class TestWaitForReconnection:
assert result.message_id == "msg_123"
+# ---------------------------------------------------------------------------
+# Regression for #78183: httpx timeout empty-string defeats _is_timeout_error
+# ---------------------------------------------------------------------------
+
+class TestQQTimeoutErrorNormalization:
+ """When an httpx timeout has an empty string representation, qqbot's send
+ paths must preserve the exception type name so the base-layer timeout guard
+ (_is_timeout_error) can still recognise it and suppress the duplicate-
+ delivery plain-text fallback."""
+
+ def _make_adapter(self, **extra):
+ from gateway.platforms.qqbot import QQAdapter
+ return QQAdapter(_make_config(app_id="a", client_secret="b", **extra))
+
+ @pytest.mark.asyncio
+ async def test_send_chunk_preserves_read_timeout_type(self):
+ from gateway.platforms.base import BasePlatformAdapter
+
+ adapter = self._make_adapter()
+
+ async def _boom(*args, **kwargs):
+ raise httpx.ReadTimeout("")
+
+ adapter._send_c2c_text = _boom
+
+ result = await adapter._send_chunk("test_openid", "hello world")
+
+ assert not result.success
+ assert result.error, "error must not be empty"
+ assert BasePlatformAdapter._is_timeout_error(result.error), (
+ f"_is_timeout_error must recognise {result.error!r}"
+ )
+
+ @pytest.mark.asyncio
+ async def test_send_chunk_non_empty_error_unchanged(self):
+ """A normal exception with a message must keep its original text."""
+ adapter = self._make_adapter()
+
+ async def _boom(*args, **kwargs):
+ raise RuntimeError("Server error '500 Internal Server Error'")
+
+ adapter._send_c2c_text = _boom
+
+ result = await adapter._send_chunk("test_openid", "hello world")
+
+ assert not result.success
+ assert "500 Internal Server Error" in (result.error or "")
+
+
# ---------------------------------------------------------------------------
# ChunkedUploader
# ---------------------------------------------------------------------------