From f2ff3e07716405431571e922080367e82da7d04c Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 15 Nov 2019 16:17:46 -0500 Subject: [PATCH] cli: print error causes in all error paths I noticed that providing a parameter with a wrong syntax to a query resulted in no error stack shown by the CLI, even though a bunch of BT_CLI_LOGE_APPEND_CAUSE are done along the way. We just get the logged errors: ./src/cli/babeltrace2 query -p 'a=2,' src.ctf.fs yo 11-15 16:17:34.734 6423 6423 E CLI/CFG-CLI-ARGS bt_config_query_from_args@babeltrace2-cfg-cli-args.c:1530 Invalid format for --params option's argument: Expecting unquoted map key: a=2, ^ 11-15 16:17:34.735 6423 6423 E CLI main@babeltrace2.c:2781 Command-line error: retcode=1 This is because the main function only prints the error stack when the command execution fails, not when anything earlier fails, like the creation of the command configuration in this case. Move the call to print_error_causes at the end, so that the causes are printed every time we exit with status code 1. I then found that the amount of vertical blank space in the result was too damn high: myprompt$ ./src/cli/babeltrace2 query -p 'a=2,' ctf src.ctf.fs yo 11-15 16:38:15.850 13799 13799 E CLI/CFG-CLI-ARGS bt_config_query_from_args@babeltrace2-cfg-cli-args.c:1530 Invalid format for --params option's argument: Expecting unquoted map key: a=2, ^ 11-15 16:38:15.851 13799 13799 E CLI main@babeltrace2.c:2781 Command-line error: retcode=1 ERROR: [Babeltrace CLI] (/home/smarchi/src/babeltrace/src/cli/babeltrace2.c:2781) Command-line error: retcode=1 CAUSED BY [Babeltrace CLI] (/home/smarchi/src/babeltrace/src/cli/babeltrace2-cfg-cli-args.c:1530) Invalid format for --params option's argument: Expecting unquoted map key: a=2, ^ myprompt$ So I removed the newlines added by ini_append_error_expecting. I think it looks readable and more concise this way. Any caller who wants the newlines can add them itself. myprompt$ ./src/cli/babeltrace2 query -p 'a=2,' ctf src.ctf.fs yo 11-15 16:41:38.600 15717 15717 E CLI/CFG-CLI-ARGS bt_config_query_from_args@babeltrace2-cfg-cli-args.c:1530 Invalid format for --params option's argument: Expecting unquoted map key: a=2, ^ 11-15 16:41:38.600 15717 15717 E CLI main@babeltrace2.c:2781 Command-line error: retcode=1 ERROR: [Babeltrace CLI] (/home/smarchi/src/babeltrace/src/cli/babeltrace2.c:2781) Command-line error: retcode=1 CAUSED BY [Babeltrace CLI] (/home/smarchi/src/babeltrace/src/cli/babeltrace2-cfg-cli-args.c:1530) Invalid format for --params option's argument: Expecting unquoted map key: a=2, ^ myprompt$ Change-Id: Id38159896d595b9c8fcac00b3364577e1ea36883 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/2394 Tested-by: jenkins --- src/cli/babeltrace2.c | 5 ++++- src/param-parse/param-parse.c | 2 +- tests/cli/query/test_query | 5 ++++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/cli/babeltrace2.c b/src/cli/babeltrace2.c index 7dfeedbd..0339426d 100644 --- a/src/cli/babeltrace2.c +++ b/src/cli/babeltrace2.c @@ -2838,7 +2838,6 @@ int main(int argc, const char **argv) break; case BT_CMD_STATUS_ERROR: retcode = 1; - print_error_causes(); break; case BT_CMD_STATUS_INTERRUPTED: retcode = 2; @@ -2849,6 +2848,10 @@ int main(int argc, const char **argv) } end: + if (retcode == 1) { + print_error_causes(); + } + BT_OBJECT_PUT_REF_AND_RESET(cfg); fini_loaded_plugins(); bt_interrupter_put_ref(the_interrupter); diff --git a/src/param-parse/param-parse.c b/src/param-parse/param-parse.c index e939e9ab..566c61ef 100644 --- a/src/param-parse/param-parse.c +++ b/src/param-parse/param-parse.c @@ -99,7 +99,7 @@ void ini_append_error_expecting(struct ini_parsing_state *state, g_string_append_c(state->ini_error, ' '); } - g_string_append_printf(state->ini_error, "^\n\n"); + g_string_append_c(state->ini_error, '^'); } static diff --git a/tests/cli/query/test_query b/tests/cli/query/test_query index 1be9fa24..803644ba 100755 --- a/tests/cli/query/test_query +++ b/tests/cli/query/test_query @@ -24,7 +24,7 @@ fi # shellcheck source=../../utils/utils.sh SH_TAP=1 source "$UTILSSH" -NUM_TESTS=11 +NUM_TESTS=15 plan_tests $NUM_TESTS @@ -92,6 +92,9 @@ expect_failure "ValueError: catastrophic failure" \ expect_failure 'Cannot find component class: plugin-name="query", comp-cls-name="NonExistentSource", comp-cls-type=1' \ 'src.query.NonExistentSource' 'the-object' '-p' 'a=2' +# Wrong parameter syntax. +expect_failure "Invalid format for --params option's argument:" \ + 'src.query.SourceWithQueryThatPrintsParams' 'please-fail' '-p' 'a=3,' rm -f "$stdout_expected_file" rm -f "$stdout_file" -- 2.34.1