Skip to content

Commit

Permalink
Add tracebacks to errors in CLI runs (#4075)
Browse files Browse the repository at this point in the history
* Add tracebacks to errors in CLI runs

Signed-off-by: Laura Couto <laurarccouto@gmail.com>

* Raise exception instead of logging it

Signed-off-by: Laura Couto <laurarccouto@gmail.com>

* Update tests

Signed-off-by: Laura Couto <laurarccouto@gmail.com>

---------

Signed-off-by: Laura Couto <laurarccouto@gmail.com>
  • Loading branch information
lrcouto authored Aug 8, 2024
1 parent 5ec27a3 commit 62431bb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
9 changes: 2 additions & 7 deletions kedro/framework/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from __future__ import annotations

import importlib
import logging
import sys
import traceback
from collections import defaultdict
Expand Down Expand Up @@ -39,9 +38,6 @@
v{version}
"""

logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler(sys.stderr))


@click.group(context_settings=CONTEXT_SETTINGS, name="Kedro")
@click.version_option(version, "--version", "-V", help="Show version and exit")
Expand Down Expand Up @@ -208,13 +204,12 @@ def main(
click.echo(message)
click.echo(hint)
sys.exit(exc.code)
except Exception as error:
logger.error(f"An error has occurred: {error}")
except Exception:
self._cli_hook_manager.hook.after_command_run(
project_metadata=self._metadata, command_args=args, exit_code=1
)
hook_called = True
sys.exit(1)
raise
finally:
if not hook_called:
self._cli_hook_manager.hook.after_command_run(
Expand Down
4 changes: 2 additions & 2 deletions tests/framework/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def test_main_hook_exception_handling(self, fake_metadata):
project_metadata=kedro_cli._metadata, command_args=[], exit_code=1
)

assert "An error has occurred: Test Exception" in result.output
assert result.exit_code == 1

@patch("sys.exit")
def test_main_hook_finally_block(self, fake_metadata):
Expand All @@ -535,7 +535,7 @@ def test_main_hook_finally_block(self, fake_metadata):
project_metadata=kedro_cli._metadata, command_args=[], exit_code=0
)

assert "An error has occurred:" not in result.output
assert result.exit_code == 0


@mark.usefixtures("chdir_to_dummy_project")
Expand Down

0 comments on commit 62431bb

Please sign in to comment.