Skip to content

Commit

Permalink
cli: print error causes in all error paths
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
simark authored and jgalar committed Nov 26, 2019
1 parent 8e4fa50 commit f2ff3e0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/cli/babeltrace2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/param-parse/param-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion tests/cli/query/test_query
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit f2ff3e0

Please sign in to comment.