Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

CLI stack traces when debugging #960

Merged
merged 2 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pantheon/src/main/java/tech/pegasys/pantheon/Pantheon.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package tech.pegasys.pantheon;

import static org.apache.logging.log4j.LogManager.getLogger;
import static picocli.CommandLine.defaultExceptionHandler;

import tech.pegasys.pantheon.cli.PantheonCommand;
import tech.pegasys.pantheon.cli.PantheonControllerBuilder;
Expand All @@ -38,7 +37,7 @@ public static void main(final String... args) {

pantheonCommand.parse(
new RunLast().andExit(SUCCESS_EXIT_CODE),
defaultExceptionHandler().andExit(ERROR_EXIT_CODE),
pantheonCommand.exceptionHandler().andExit(ERROR_EXIT_CODE),
System.in,
args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import picocli.CommandLine;
import picocli.CommandLine.AbstractParseResultHandler;
import picocli.CommandLine.DefaultExceptionHandler;
import picocli.CommandLine.ExecutionException;
import picocli.CommandLine.Model.OptionSpec;
import picocli.CommandLine.ParseResult;
Expand All @@ -27,13 +26,13 @@ class ConfigOptionSearchAndRunHandler extends AbstractParseResultHandler<List<Ob
private static final String DOCKER_CONFIG_LOCATION = "/etc/pantheon/pantheon.conf";

private final AbstractParseResultHandler<List<Object>> resultHandler;
private final DefaultExceptionHandler<List<Object>> exceptionHandler;
private final CommandLine.IExceptionHandler2<List<Object>> exceptionHandler;
private final String configFileOptionName;
private final boolean isDocker;

ConfigOptionSearchAndRunHandler(
final AbstractParseResultHandler<List<Object>> resultHandler,
final DefaultExceptionHandler<List<Object>> exceptionHandler,
final CommandLine.IExceptionHandler2<List<Object>> exceptionHandler,
final String configFileOptionName,
final boolean isDocker) {
this.resultHandler = resultHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

import com.google.common.base.Suppliers;
import com.google.common.io.Resources;
import io.vertx.core.Vertx;
import io.vertx.core.json.DecodeException;
Expand All @@ -82,7 +84,6 @@
import picocli.CommandLine;
import picocli.CommandLine.AbstractParseResultHandler;
import picocli.CommandLine.Command;
import picocli.CommandLine.DefaultExceptionHandler;
import picocli.CommandLine.ExecutionException;
import picocli.CommandLine.ITypeConverter;
import picocli.CommandLine.Option;
Expand Down Expand Up @@ -458,6 +459,39 @@ private Long configureRefreshDelay(final Long refreshDelay) {
"The address to which the privacy pre-compiled contract will be mapped to (default: ${DEFAULT-VALUE})")
private final Integer privacyPrecompiledAddress = Address.PRIVACY;

// Inner class so we can get to loggingLevel.
public class PantheonExceptionHandler
extends CommandLine.AbstractHandler<List<Object>, PantheonExceptionHandler>
implements CommandLine.IExceptionHandler2<List<Object>> {

@Override
public List<Object> handleParseException(final ParameterException ex, final String[] args) {
if (logLevel != null && Level.DEBUG.isMoreSpecificThan(logLevel)) {
ex.printStackTrace(err());
} else {
err().println(ex.getMessage());
}
if (!CommandLine.UnmatchedArgumentException.printSuggestions(ex, err())) {
ex.getCommandLine().usage(err(), ansi());
}
return returnResultOrExit(null);
}

@Override
public List<Object> handleExecutionException(
final ExecutionException ex, final CommandLine.ParseResult parseResult) {
return throwOrExit(ex);
}

@Override
protected PantheonExceptionHandler self() {
return this;
}
}

private Supplier<PantheonExceptionHandler> exceptionHandlerSupplier =
Suppliers.memoize(PantheonExceptionHandler::new);

public PantheonCommand(
final Logger logger,
final BlockImporter blockImporter,
Expand All @@ -475,7 +509,7 @@ public PantheonCommand(

public void parse(
final AbstractParseResultHandler<List<Object>> resultHandler,
final DefaultExceptionHandler<List<Object>> exceptionHandler,
final PantheonExceptionHandler exceptionHandler,
final InputStream in,
final String... args) {

Expand Down Expand Up @@ -578,7 +612,7 @@ public void run() {
metricsConfiguration(),
permissioningConfiguration);
} catch (Exception e) {
throw new ParameterException(this.commandLine, e.getMessage());
throw new ParameterException(this.commandLine, e.getMessage(), e);
}
}

Expand Down Expand Up @@ -1028,4 +1062,8 @@ private boolean isFullInstantiation() {
public MetricsSystem getMetricsSystem() {
return metricsSystem;
}

public PantheonExceptionHandler exceptionHandler() {
return exceptionHandlerSupplier.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.net.URI;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -51,7 +50,6 @@
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import picocli.CommandLine;
import picocli.CommandLine.DefaultExceptionHandler;
import picocli.CommandLine.Help.Ansi;
import picocli.CommandLine.RunLast;

Expand Down Expand Up @@ -159,7 +157,7 @@ protected CommandLine.Model.CommandSpec parseCommand(final InputStream in, final
// parse using Ansi.OFF to be able to assert on non formatted output results
pantheonCommand.parse(
new RunLast().useOut(outPrintStream).useAnsi(Ansi.OFF),
new DefaultExceptionHandler<List<Object>>().useErr(errPrintStream).useAnsi(Ansi.OFF),
pantheonCommand.exceptionHandler().useErr(errPrintStream).useAnsi(Ansi.OFF),
in,
args);
return pantheonCommand.spec;
Expand Down