Skip to content

Commit

Permalink
Fixed a bug that causes the exit message to be dropped and the CLI to…
Browse files Browse the repository at this point in the history
…ol to exit silently with an error code.
  • Loading branch information
david-waltermire committed Nov 4, 2024
1 parent 0859d4a commit a950ac1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode)

void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode,
@NonNull Class<? extends Throwable> thrownClass) {
status.generateMessage(true);
Throwable thrown = status.getThrowable();
assertAll(
() -> assertEquals(expectedCode, status.getExitCode(), "exit code mismatch"),
Expand Down

0 comments on commit a950ac1

Please sign in to comment.