Skip to content

Commit

Permalink
Improve error message on invalid user_config_json
Browse files Browse the repository at this point in the history
When the parameter value was not a valid json, the error message was empty.

    ERROR	command failed: UserError:

Add a detailed error message showing the location of the error:

    ERROR	command failed: UserError: Invalid user_config_json: Expecting value: line 8 column 13 (char 202)
  • Loading branch information
kmichel-aiven committed Sep 28, 2022
1 parent 1ae7f87 commit 2c1ca76
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion aiven/client/cliarg.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def wrapped(self: CommandLineTool) -> T:
get_json_config(self.args.user_config_json),
)
except jsonlib.decoder.JSONDecodeError as err:
raise UserError from err
raise UserError(f"Invalid user_config_json: {err!s}") from err
return fun(self)

return wrapped
Expand Down
10 changes: 8 additions & 2 deletions tests/test_cliarg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
# See the file `LICENSE` for details.

# pylint: disable=no-member
from _pytest.logging import LogCaptureFixture
from aiven.client.argx import CommandLineTool
from aiven.client.cliarg import arg # type: ignore


def test_user_config_json_error_json() -> None:
def test_user_config_json_error_json(caplog: LogCaptureFixture) -> None:
"""Test that @arg.user_config_json causes
CommandLineTool.run() to exit cleanly with return value 1
if JSON is incorrect
Expand All @@ -22,9 +23,14 @@ class T(CommandLineTool):
def t(self) -> None:
"""t"""

error_json_arg = ["t", "--user-config-json", "foo"]
error_json_arg = ["t", "--user-config-json", "bar"]
test_class = T("avn")
ret = test_class.run(args=error_json_arg)
# The message can vary across python versions
assert (
"Invalid user_config_json: Expecting value: line 1 column 1 (char 0)" in caplog.text
or "Invalid user_config_json: Unexpected 'b': line 1 column 1 (char 0)" in caplog.text
)
assert ret == 1


Expand Down

0 comments on commit 2c1ca76

Please sign in to comment.