From 730dafc4744fe0f03c7b2cfc98c63c814d563d68 Mon Sep 17 00:00:00 2001 From: wolfgang-aura <169568318+wolfgang-aura@users.noreply.github.com> Date: Sun, 11 Oct 2026 09:47:23 +0800 Subject: [PATCH] Include the broker error in SendTaskError AsyncKicker.kiq wrapped every broker failure in a SendTaskError whose message was always "Cannot send task to the queue". The cause was only visible in the chained traceback, so logs that print str(exc) lost it. The message now ends with the underlying exception type and text. --- taskiq/exceptions.py | 3 ++- taskiq/kicker.py | 2 +- tests/test_kicker.py | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/taskiq/exceptions.py b/taskiq/exceptions.py index 1e6fbb83..78c2f097 100644 --- a/taskiq/exceptions.py +++ b/taskiq/exceptions.py @@ -35,7 +35,8 @@ class SharedBrokerListenError(ListenError): class SendTaskError(BrokerError): """Error if the broker was unable to send the task to the queue.""" - __template__ = "Cannot send task to the queue" + __template__ = "Cannot send task to the queue: {reason}" + reason: str = "unknown reason" class SharedBrokerSendTaskError(SendTaskError): diff --git a/taskiq/kicker.py b/taskiq/kicker.py index 07f4e51a..6fe5d0ed 100644 --- a/taskiq/kicker.py +++ b/taskiq/kicker.py @@ -165,7 +165,7 @@ async def kiq( try: await self.broker.kick(self.broker.formatter.dumps(message)) except Exception as exc: - raise SendTaskError from exc + raise SendTaskError(reason=f"{type(exc).__name__}: {exc}") from exc for middleware in reversed(self.broker.middlewares): if middleware.__class__.post_send != TaskiqMiddleware.post_send: diff --git a/tests/test_kicker.py b/tests/test_kicker.py index 05afc0cb..7cfac4cd 100644 --- a/tests/test_kicker.py +++ b/tests/test_kicker.py @@ -1,6 +1,9 @@ from typing import Any +import pytest + from taskiq import InMemoryBroker +from taskiq.exceptions import SendTaskError from taskiq.kicker import AsyncKicker @@ -43,3 +46,23 @@ async def test_other_labels_still_serialized() -> None: assert message.labels["retries"] == "3" assert message.labels["queue"] == "high_priority" + + +async def test_send_task_error_includes_broker_failure() -> None: + """SendTaskError message names the underlying broker exception.""" + broker = InMemoryBroker() + + async def failing_kick(message: Any) -> None: + raise ConnectionError("queue is unreachable") + + broker.kick = failing_kick # type: ignore[method-assign] + + @broker.task + async def run_task() -> None: + pass + + with pytest.raises(SendTaskError) as exc_info: + await run_task.kiq() + + assert "ConnectionError: queue is unreachable" in str(exc_info.value) + assert isinstance(exc_info.value.__cause__, ConnectionError)