From a950ac1e7ae889445221deaa62040030b5d95546 Mon Sep 17 00:00:00 2001 From: David Waltermire Date: Mon, 4 Nov 2024 17:50:25 -0500 Subject: [PATCH] Fixed a bug that causes the exit message to be dropped and the CLI tool to exit silently with an error code. --- .../cli/processor/CLIProcessor.java | 19 ++++++++++--------- .../commands/AbstractConvertSubcommand.java | 4 +++- .../nist/secauto/metaschema/cli/CLITest.java | 1 - 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java index bfbcc84fa..823d814e2 100644 --- a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java +++ b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java @@ -413,17 +413,14 @@ public ExitStatus processCommand() { if (cmdLine.hasOption(QUIET_OPTION)) { handleQuiet(); } - ExitStatus retval = invokeCommand(cmdLine); - if (ExitCode.OK.equals(retval.getExitCode())) { - handleError(retval, cmdLine, false); - } - return retval; + return invokeCommand(cmdLine); } @SuppressWarnings({ "PMD.OnlyOneReturn", // readability "PMD.AvoidCatchingGenericException" // needed here }) + @NonNull protected ExitStatus invokeCommand(@NonNull CommandLine cmdLine) { ExitStatus retval; try { @@ -443,15 +440,19 @@ protected ExitStatus invokeCommand(@NonNull CommandLine cmdLine) { .withThrowable(ex); } } - - if (ExitCode.INVALID_COMMAND.equals(retval.getExitCode())) { - showHelp(); - } } catch (RuntimeException ex) { retval = ExitCode.RUNTIME_ERROR .exitMessage(String.format("An uncaught runtime error occurred. %s", ex.getLocalizedMessage())) .withThrowable(ex); } + + if (!ExitCode.OK.equals(retval.getExitCode())) { + retval.generateMessage(cmdLine.hasOption(SHOW_STACK_TRACE_OPTION)); + + if (ExitCode.INVALID_COMMAND.equals(retval.getExitCode())) { + showHelp(); + } + } return retval; } diff --git a/metaschema-cli/src/main/java/gov/nist/secauto/metaschema/cli/commands/AbstractConvertSubcommand.java b/metaschema-cli/src/main/java/gov/nist/secauto/metaschema/cli/commands/AbstractConvertSubcommand.java index 5921f4d74..a0049ce58 100644 --- a/metaschema-cli/src/main/java/gov/nist/secauto/metaschema/cli/commands/AbstractConvertSubcommand.java +++ b/metaschema-cli/src/main/java/gov/nist/secauto/metaschema/cli/commands/AbstractConvertSubcommand.java @@ -145,8 +145,10 @@ public void execute() throws CommandExecutionException { handleConversion(source, toFormat, writer, loader); } } - } catch (IOException | IllegalArgumentException ex) { + } catch (IllegalArgumentException ex) { throw new CommandExecutionException(ExitCode.PROCESSING_ERROR, ex); + } catch (IOException ex) { + throw new CommandExecutionException(ExitCode.IO_ERROR, ex); } if (destination != null && LOGGER.isInfoEnabled()) { LOGGER.info("Generated {} file: {}", toFormat.toString(), destination); diff --git a/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java b/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java index 673fef7ea..0e4779e04 100644 --- a/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java +++ b/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java @@ -39,7 +39,6 @@ void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode) void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode, @NonNull Class thrownClass) { - status.generateMessage(true); Throwable thrown = status.getThrowable(); assertAll( () -> assertEquals(expectedCode, status.getExitCode(), "exit code mismatch"),