cli: print error causes in all error paths
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 15 Nov 2019 21:17:46 +0000 (16:17 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 26 Nov 2019 19:24:17 +0000 (14:24 -0500)
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 <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2394
Tested-by: jenkins <jenkins@lttng.org>
src/cli/babeltrace2.c
src/param-parse/param-parse.c
tests/cli/query/test_query

index 7dfeedbdb3091252d8c447d312e3e40abe0acb1b..0339426dc4e7c8f33f037679ead7eb2cc6520ba8 100644 (file)
@@ -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);
index e939e9abc606a83da5a0ff9bdd87429f367c466a..566c61efea56761def7039812a811d41ae0747ef 100644 (file)
@@ -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
index 1be9fa24287218249173c7772c1f6ea6928a8a33..803644ba462a942e6a7312c4f853d5c6788ea1e5 100755 (executable)
@@ -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"
This page took 0.028086 seconds and 4 git commands to generate.