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

settings command fails ungracefully when file does not exist #295

Open
cdsre opened this issue Mar 3, 2024 · 0 comments
Open

settings command fails ungracefully when file does not exist #295

cdsre opened this issue Mar 3, 2024 · 0 comments
Labels
pending-review Waiting for a first review

Comments

@cdsre
Copy link
Contributor

cdsre commented Mar 3, 2024

Describe the bug
When running the chaos settings command either with the default config or passing a settings file an unhandled exception occurs.

 chaos --settings foo.json settings show --format json

Traceback (most recent call last):
  File "C:\Users\doyle\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\doyle\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\Scripts\chaos.exe\__main__.py", line 7, in <module>
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "D:\jetbrains\PycharmProjects\chaostoolkit\chaostoolkit\commands\settings.py", line 39, in show_settings
    click.abort(
AttributeError: module 'click' has no attribute 'abort'. Did you mean: 'Abort'?

Runtime versions

(ctk) PS D:\jetbrains\PycharmProjects\chaostoolkit> python --version
Python 3.10.4
(ctk) PS D:\jetbrains\PycharmProjects\chaostoolkit> chaos info core
NAME                VERSION
CLI                 1.19.1.dev1+g10f7d4c.d20240303.editable
Core library        1.42.0
(ctk) PS D:\jetbrains\PycharmProjects\chaostoolkit> chaos info extensions
NAME                                    VERSION   LICENSE                       DESCRIPTION

To Reproduce
Just run the command when you do not have the default ~/.chaostoolkit/settings.yaml or if that's set run the command specifically passing a settings file that doesn't exist

(ctk) PS D:\jetbrains\PycharmProjects\chaostoolkit> chaos --settings foo.json settings show --format json
...
...
  File "D:\jetbrains\PycharmProjects\chaostoolkit\chaostoolkit\commands\settings.py", line 39, in show_settings
    click.abort(
AttributeError: module 'click' has no attribute 'abort'. Did you mean: 'Abort'?

Expected behavior
The command should fail gracefully as the code intended.

@click.pass_context
def show_settings(ctx: click.Context, fmt: str = "json"):
    """
    Show the entire content of the settings file.

    Be aware this will not obfuscate secret data.
    """
    if not os.path.isfile(ctx.obj["settings_path"]):
        click.abort(
            "No settings file found at {}".format(ctx.obj["settings_path"])
        )

    settings = load_settings(ctx.obj["settings_path"]) or {}
    if fmt == "json":
        click.echo(json.dumps(settings, indent=2))
    elif fmt == "yaml":
        click.echo(yaml.dump(settings, indent=2))

Additional context
The issue here is that click.abort is not exported via the __init__.py of the click package. It is actually click.Abort however I dont believe that will have the desired affect in this case since that's just the exception type. Instead i would suggest to use ctx.fail

    if not os.path.isfile(ctx.obj["settings_path"]):
        ctx.fail(
            "No settings file found at {}".format(ctx.obj["settings_path"])
        )

This will then give the right experience both with the default or a custom file

(ctk) PS D:\jetbrains\PycharmProjects\chaostoolkit> chaos settings show --format json
Usage: chaos settings show [OPTIONS]
Try 'chaos settings show --help' for help.

Error: No settings file found at C:\Users\doyle\.chaostoolkit\settings.yaml
(ctk) PS D:\jetbrains\PycharmProjects\chaostoolkit> chaos --settings my/settings/files/foo.json settings show --format json
Usage: chaos settings show [OPTIONS]
Try 'chaos settings show --help' for help.

Error: No settings file found at my/settings/files/foo.json
@cdsre cdsre added the pending-review Waiting for a first review label Mar 3, 2024
cdsre added a commit to cdsre/chaostoolkit that referenced this issue Mar 3, 2024
…d would cause an uncaught exception. The code was designed to handle this but was using the wrong method. Also added a test for this. This fixes issue chaostoolkit#295

Signed-off-by: Chris Doyle <uk.chris.doyle+cdsre@gmail.com>
cdsre added a commit to cdsre/chaostoolkit that referenced this issue Mar 3, 2024
…d would cause an uncaught exception. The code was designed to handle this but was using the wrong method. Also added a test for this. This fixes issue chaostoolkit#295

Signed-off-by: Chris Doyle <uk.chris.doyle+cdsre@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending-review Waiting for a first review
Projects
None yet
Development

No branches or pull requests

1 participant