Skip to content

Commit

Permalink
hide CommandFailedException stacktrace when thrown to set exit code (…
Browse files Browse the repository at this point in the history
…DAT-9608) (#2938)

Stacktraces will not be printed to the logs when a CommandFailedException is set to "expected".
  • Loading branch information
StevenMassaro committed Jun 17, 2022
1 parent e43bf18 commit c1c7673
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@ private int handleException(Throwable exception) {
bestMessage = bestMessage.replace("Unexpected error running Liquibase: ", "");
}

Scope.getCurrentScope().getLog(getClass()).severe(bestMessage, exception);
if (cause instanceof CommandFailedException && ((CommandFailedException) cause).isExpected()) {
Scope.getCurrentScope().getLog(getClass()).severe(bestMessage);
} else {
Scope.getCurrentScope().getLog(getClass()).severe(bestMessage, exception);
}

boolean printUsage = false;
try (final StringWriter suggestionWriter = new StringWriter();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package liquibase.command;

/**
* CommandFailedException is thrown any time a command did not succeed. If it did not succeed due to normal and expected
* reasons, mark it as expected=true. If the CommandFailedException is marked as expected=false, the code calling the
* command may want to do additional logging or handling of the exception because it knows the command was surprised by the result.
*/
public class CommandFailedException extends Exception {
private final CommandResults results;
private final int exitCode;
private final boolean expected;

public CommandFailedException(CommandResults results, int exitCode, String message) {
public CommandFailedException(CommandResults results, int exitCode, String message, boolean expected) {
super(message);
this.results = results;
this.exitCode = exitCode;
this.expected = expected;
}

public CommandResults getResults() {
Expand All @@ -17,4 +24,13 @@ public CommandResults getResults() {
public int getExitCode() {
return exitCode;
}

/**
* In certain circumstances, this exception is thrown solely to set the exit code of a command's execution, in which
* case, the exception is expected. In these cases, Liquibase does not print the stacktrace of the exception to the
* logs, and isExpected returns true.
*/
public boolean isExpected() {
return expected;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package liquibase.command;

import liquibase.Scope;
import liquibase.precondition.FailedPrecondition;
import liquibase.util.StringUtil;

import java.io.OutputStream;
Expand Down Expand Up @@ -56,7 +57,11 @@ public <T> CommandResultsBuilder addResult(CommandResultDefinition<T> definition
}

public CommandFailedException commandFailed(String message, int exitCode) {
return new CommandFailedException(this.build(), exitCode, message);
return commandFailed(message, exitCode, false);
}

public CommandFailedException commandFailed(String message, int exitCode, boolean expected) {
return new CommandFailedException(this.build(), exitCode, message, expected);
}

/**
Expand Down

0 comments on commit c1c7673

Please sign in to comment.