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)