Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communicate Inner Task Exceptions as Part of GokartBuildError #418

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions gokart/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def __exit__(self, exception_type, exception_value, traceback):


class GokartBuildError(Exception):
pass
def __init__(self, messsage, raised_exceptions: dict[str, list[Exception]]):
super().__init__(messsage)
self.raised_exceptions = raised_exceptions


class HasLockedTaskException(Exception):
Expand Down Expand Up @@ -133,14 +135,16 @@ def build(
"""
if reset_register:
_reset_register()

with LoggerConfig(level=log_level):
task_lock_exception_raised = TaskLockExceptionRaisedFlag()
raised_exceptions: dict[str, list[Exception]] = dict()

@TaskOnKart.event_handler(luigi.Event.FAILURE)
def when_failure(task, exception):
if isinstance(exception, TaskLockException):
task_lock_exception_raised.flag = True
else:
raised_exceptions.setdefault(task.make_unique_id(), []).append(exception)

@backoff.on_exception(
partial(backoff.expo, max_value=task_lock_exception_max_wait_seconds), HasLockedTaskException, max_tries=task_lock_exception_max_tries
Expand All @@ -157,7 +161,7 @@ def _build_task():
if task_lock_exception_raised.flag:
raise HasLockedTaskException()
if result.status == luigi.LuigiStatusCode.FAILED:
raise GokartBuildError(result.summary_text)
raise GokartBuildError(result.summary_text, raised_exceptions=raised_exceptions)
return _get_output(task) if return_value else None

return _build_task()
15 changes: 15 additions & 0 deletions test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ def test_load_requires(self):
output = gokart.build(_LoadRequires(task=_DummyTask(param=text)), reset_register=False)
self.assertEqual(output, text)

def test_build_with_child_task_error(self):
class CheckException(Exception):
pass

class FailTask(gokart.TaskOnKart):
def run(self):
raise CheckException()

try:
t = FailTask()
gokart.build(t, reset_register=False, log_level=logging.CRITICAL)
except GokartBuildError as e:
self.assertEqual(len(e.raised_exceptions), 1)
self.assertIsInstance(e.raised_exceptions[t.make_unique_id()][0], CheckException)


class LoggerConfigTest(unittest.TestCase):
def test_logger_config(self):
Expand Down
Loading