From e78e473be3d131d786e2b50b854466390d4916a9 Mon Sep 17 00:00:00 2001 From: Stan Triepels <1939656+GDay@users.noreply.github.com> Date: Wed, 21 Dec 2022 01:44:59 +0100 Subject: [PATCH] Fix: handling exceptions inside job function (#51) --- django_q/cluster.py | 13 +++---------- django_q/tests/test_cluster.py | 4 ++-- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/django_q/cluster.py b/django_q/cluster.py index ba3408a..235bd64 100644 --- a/django_q/cluster.py +++ b/django_q/cluster.py @@ -485,19 +485,12 @@ def worker( try: res = f(*task["args"], **task["kwargs"]) result = (res, True) - except Exception: - result = ( - _( - "Could not process '%(func_name)s'. Check the location of the " - "function and the args/kwargs." - ) - % {"func_name": func_name}, - False, - ) + except Exception as e: + result = (f"{e} : {traceback.format_exc()}", False) if error_reporter: error_reporter.report() if task.get("sync", False): - raise Exception(result) + raise with timer.get_lock(): # Process result task["result"] = result[0] diff --git a/django_q/tests/test_cluster.py b/django_q/tests/test_cluster.py index e973c67..da240e8 100644 --- a/django_q/tests/test_cluster.py +++ b/django_q/tests/test_cluster.py @@ -29,7 +29,7 @@ from django_q.tasks import ( result, result_group, ) -from django_q.tests.tasks import multiply +from django_q.tests.tasks import multiply, TaskError from django_q.utils import add_months, add_years myPath = os.path.dirname(os.path.abspath(__file__)) @@ -64,7 +64,7 @@ def test_sync(broker): @pytest.mark.django_db def test_sync_raise_exception(broker): - with pytest.raises(Exception): + with pytest.raises(TaskError): async_task("django_q.tests.tasks.raise_exception", broker=broker, sync=True)