Skip to content

Commit

Permalink
Unhide cookiecutter errors in kedro new (#3693)
Browse files Browse the repository at this point in the history
* Added cookiecutter exceptions output

Signed-off-by: Elena Khaustova <[email protected]>

* Pre-commit checks

Signed-off-by: Elena Khaustova <[email protected]>

* Added release note

Signed-off-by: Elena Khaustova <[email protected]>

* Fixed types for python3.8

Signed-off-by: Elena Khaustova <[email protected]>

* Fixed VERBOSE_EXISTS redefining

Signed-off-by: Elena Khaustova <[email protected]>

* Moved VERBOSE_EXISTS reset to end of the test that changes it

Signed-off-by: Elena Khaustova <[email protected]>

* Fixed typos

Signed-off-by: Elena Khaustova <[email protected]>

---------

Signed-off-by: Elena Khaustova <[email protected]>
  • Loading branch information
ElenaKhaustova authored Mar 14, 2024
1 parent 12bb1dd commit 9efca0e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Upcoming Release 0.19.4

## Major features and improvements
* Cookiecutter errors are shown in short format without the `--verbose` flag.
* Kedro commands now work from any subdirectory within a Kedro project.
* Kedro CLI now provides a better error message when project commands are run outside of a project i.e. `kedro run`

Expand Down
10 changes: 8 additions & 2 deletions kedro/framework/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,21 @@ class KedroCliError(click.exceptions.ClickException):

VERBOSE_ERROR = False
VERBOSE_EXISTS = True
COOKIECUTTER_EXCEPTIONS_PREFIX = "cookiecutter.exceptions"

def show(self, file: IO | None = None) -> None:
if self.VERBOSE_ERROR:
click.secho(traceback.format_exc(), nl=False, fg="yellow")
elif self.VERBOSE_EXISTS:
etype, value, _ = sys.exc_info()
etype, value, tb = sys.exc_info()
formatted_exception = "".join(traceback.format_exception_only(etype, value))
cookiecutter_exception = ""
for ex_line in traceback.format_exception(etype, value, tb):
if self.COOKIECUTTER_EXCEPTIONS_PREFIX in ex_line:
cookiecutter_exception = ex_line
break
click.secho(
f"{formatted_exception}Run with --verbose to see the full exception",
f"{cookiecutter_exception}{formatted_exception}Run with --verbose to see the full exception",
fg="yellow",
)
else:
Expand Down
1 change: 1 addition & 0 deletions tests/framework/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ def test_run_with_invalid_config(
"Key `node-names` in provided configuration is not valid. \n\nDid you mean one of "
"these?\n node_names\n to_nodes\n namespace" in result.stdout
)
KedroCliError.VERBOSE_EXISTS = True

@mark.parametrize(
"fake_run_config_with_params,expected",
Expand Down
9 changes: 9 additions & 0 deletions tests/framework/cli/test_starters.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,15 @@ def test_fail_if_dir_exists(self, fake_kedro_cli):
assert result.exit_code != 0
assert "directory already exists" in result.output

def test_cookiecutter_exception_if_no_verbose(self, fake_kedro_cli):
"""Check if the original cookiecutter exception is present in the output
if no verbose flag is provided."""
Path("new-kedro-project").mkdir()
result = CliRunner().invoke(
fake_kedro_cli, ["new"], input=_make_cli_prompt_input()
)
assert "cookiecutter.exceptions" in result.output

def test_prompt_no_title(self, fake_kedro_cli):
shutil.copytree(TEMPLATE_PATH, "template")
_write_yaml(Path("template") / "prompts.yml", {"repo_name": {}})
Expand Down

0 comments on commit 9efca0e

Please sign in to comment.