Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hide CommandFailedException stacktrace when thrown to set exit code (DAT-9608) #2938

Merged
merged 3 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,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).isHidden()) {
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
Expand Up @@ -3,11 +3,13 @@
public class CommandFailedException extends Exception {
private final CommandResults results;
private final int exitCode;
private final boolean hidden;
StevenMassaro marked this conversation as resolved.
Show resolved Hide resolved

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

public CommandResults getResults() {
Expand All @@ -17,4 +19,12 @@ 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 these
* cases, Liquibase does not print the stacktrace of the exception to the logs, and isHidden returns true.
*/
public boolean isHidden() {
return hidden;
}
}
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 hidden) {
return new CommandFailedException(this.build(), exitCode, message, hidden);
}

/**
Expand Down