Pārlūkot izejas kodu

Report send() failures instead of a phantom success

The post_message() handlers set last_error and broke out of the chunk
loop, but the check for it sat inside the loop body after the break and
was unreachable. A completely failed send returned success=True with an
empty message_id.

Also populate raw_response, log partial delivery when a later chunk
fails, and guard follow_thread() so a best-effort call cannot fail an
otherwise successful send.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Paul-Dieter Klumpp 1 nedēļu atpakaļ
vecāks
revīzija
49e16d3419
1 mainītis faili ar 22 papildinājumiem un 6 dzēšanām
  1. 22 6
      adapter.py

+ 22 - 6
adapter.py

@@ -698,7 +698,7 @@ class ChattoAdapter(BasePlatformAdapter):
         use_auto_thread = self.chatto_config.auto_thread.value and not thread_id and not is_dm
 
         message_ids: List[str] = []
-        last_resp: Optional[dict] = None
+        last_resp: Optional[Any] = None
         last_error: Optional[str] = None
         retryable = False
 
@@ -723,9 +723,7 @@ class ChattoAdapter(BasePlatformAdapter):
                 retryable = True
                 break
 
-            if last_error and not message_ids:
-                return SendResult(success=False, error=last_error, retryable=retryable)
-            
+            last_resp = msg_obj
             self._mark_seen(msg_obj.id)
             message_ids.append(msg_obj.id)
             self._our_message_ids.add(msg_obj.id)
@@ -738,13 +736,31 @@ class ChattoAdapter(BasePlatformAdapter):
             if use_auto_thread and i == 0 and not thread_id:
                 thread_id = msg_obj.id
 
-        first_id = message_ids[0] if message_ids else ""
+        # Nothing got through at all — report the failure instead of a phantom success.
+        if not message_ids:
+            return SendResult(
+                success=False,
+                error=last_error or "Chatto: message could not be sent",
+                retryable=retryable,
+            )
+
+        first_id = message_ids[0]
 
         # ------------------------------------------------------------------ #
         # Thread following (best-effort, Chatto-unique)
         # ------------------------------------------------------------------ #
-        if thread_id and message_ids:
+        if thread_id:
+            try:
                 await client.follow_thread(chat_id, thread_id)
+            except Exception:
+                logger.debug("Chatto: follow_thread failed for %s/%s", chat_id, thread_id, exc_info=True)
+
+        # A later chunk failed after earlier ones went out: partial delivery.
+        if last_error:
+            logger.warning(
+                "Chatto: sent %d/%d chunk(s) to %s before failing: %s",
+                len(message_ids), len(chunks), chat_id, last_error,
+            )
 
         return SendResult(success=True, message_id=first_id, raw_response=last_resp)