Skip to content

Commit 7c5e0d3

Browse files
committed
fix: Task exception was never retrieved if return annotation validation fails
1 parent 34dcdf6 commit 7c5e0d3

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/sheppy/utils/task_execution.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@ async def execute_task(__task: "Task", worker_id: str) -> tuple[Exception | None
7979
except Exception as e:
8080
raise Exception("Middleware error") from e
8181

82-
return exception, task.create_task()
82+
# result validation might fail
83+
try:
84+
final_task = task.create_task()
85+
except Exception as e:
86+
task = await TaskProcessor.handle_failed_task(task, e)
87+
exception = e
88+
final_task = task.create_task()
89+
90+
return exception, final_task
8391

8492
@staticmethod
8593
async def process_pre_task_middleware(task: TaskInternal) -> tuple[TaskInternal, list[Any]]:
@@ -123,6 +131,7 @@ def handle_success_and_update_task_metadata(task: TaskInternal, result: Any, wor
123131
@staticmethod
124132
async def handle_failed_task(task: TaskInternal, exception: Exception) -> TaskInternal:
125133
task.completed = False
134+
task.result = None
126135
task.error = f"{exception.__class__.__name__}: {exception}"
127136

128137
if task.is_retriable:

tests/contract/test_edgecases.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from sheppy import Queue, Worker, task
2+
from sheppy.testqueue import assert_is_failed
3+
4+
5+
@task
6+
async def task_with_wrong_return_annotation() -> list:
7+
return 5
8+
9+
10+
async def test_wrong_return_annotation(queue: Queue, worker: Worker) -> None:
11+
t = task_with_wrong_return_annotation()
12+
13+
await queue.add(t)
14+
await worker.work(1)
15+
16+
t = await queue.get_task(t)
17+
18+
assert_is_failed(t)

0 commit comments

Comments
 (0)