throw ex; } catch (Exception ex) { throw new ExecutionException(parsed, "Error while running command (" + command + "): " + ex, ex); throw ex; } catch (Exception ex) { throw new ExecutionException(parsed, "Error while calling command (" + command + "): " + ex, ex); throw (ExecutionException) t; } else { throw new ExecutionException(parsed, "Error while calling command (" + command + "): " + t, t); throw new ExecutionException(parsed, "Unhandled error while calling command (" + command + "): " + ex, ex); throw new ExecutionException(parsed, "Parsed command (" + command + ") is not Method, Runnable or Callable");
@Test public void testRunWithFactory() { Runnable[] variations = new Runnable[] { new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), "-x", "a");}}, new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), System.out, "-x", "a");}}, new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), System.out, Help.Ansi.OFF, "-x", "a");}}, new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), System.out, System.out, Help.Ansi.OFF, "-x", "a");}}, }; for (Runnable r : variations) { try { r.run(); } catch (ExecutionException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Error while running command (picocli.CommandLineParseWithHandlersTest$MyRunnable")); assertTrue(ex.getCause() instanceof IllegalStateException); assertEquals("this is a test", ex.getCause().getMessage()); } } }
@Test public void testCall1DefaultExceptionHandlerRethrows() { try { CommandLine.call(new MyCallable(), "-x abc"); } catch (ExecutionException ex) { String cmd = ex.getCommandLine().getCommand().toString(); String msg = "Error while calling command (" + cmd + "): java.lang.IllegalStateException: this is a test"; assertEquals(msg, ex.getMessage()); } assertEquals("", systemErrRule.getLog()); assertEquals("", systemOutRule.getLog()); }
throw ex; } catch (Exception ex) { throw new ExecutionException(parsed, "Error while running command (" + command + "): " + ex, ex); throw ex; } catch (Exception ex) { throw new ExecutionException(parsed, "Error while calling command (" + command + "): " + ex, ex); throw (ExecutionException) t; } else { throw new ExecutionException(parsed, "Error while calling command (" + command + "): " + t, t); throw new ExecutionException(parsed, "Unhandled error while calling command (" + command + "): " + ex, ex); throw new ExecutionException(parsed, "Parsed command (" + command + ") is not Method, Runnable or Callable");
path, parentCommand.parentCommand.buildController()); } catch (final FileNotFoundException e) { throw new ExecutionException( new CommandLine(this), "Could not find file to import: " + blocksImportFile); } catch (final IOException e) { throw new ExecutionException( new CommandLine(this), "Unable to import blocks from " + blocksImportFile, e); } finally {
@Override protected List<Object> handle(final ParseResult parseResult) throws ExecutionException { final CommandLine commandLine = parseResult.asCommandLineList().get(0); if (parseResult.hasMatchedOption(configFileOptionName)) { final OptionSpec configFileOption = parseResult.matchedOption(configFileOptionName); final File configFile; try { configFile = configFileOption.getter().get(); } catch (final Exception e) { throw new ExecutionException(commandLine, e.getMessage(), e); } final TomlConfigFileDefaultProvider tomlConfigFileDefaultProvider = new TomlConfigFileDefaultProvider(commandLine, configFile); commandLine.setDefaultValueProvider(tomlConfigFileDefaultProvider); } else if (isDocker) { final File configFile = new File(DOCKER_CONFIG_LOCATION); if (configFile.exists()) { final TomlConfigFileDefaultProvider tomlConfigFileDefaultProvider = new TomlConfigFileDefaultProvider(commandLine, configFile); commandLine.setDefaultValueProvider(tomlConfigFileDefaultProvider); } } commandLine.parseWithHandlers( resultHandler, exceptionHandler, parseResult.originalArgs().toArray(new String[0])); return new ArrayList<>(); }
@Test public void testHandlerThrowsExecutionException2() { @Command class App { } IParseResultHandler2<Void> handler = new IParseResultHandler2<Void>() { public Void handleParseResult(ParseResult parseResult) throws ExecutionException { throw new ExecutionException(new CommandLine(new App()), "xyz"); } }; IExceptionHandler2<Void> exceptionHandler = new IExceptionHandler2<Void>() { public Void handleParseException(ParameterException ex, String[] args) { return null; } public Void handleExecutionException(ExecutionException ex, ParseResult parseResult) { return null; } }; try { new CommandLine(new App()).parseWithHandlers(handler, exceptionHandler, new String[0]); } catch (ExecutionException ex) { assertEquals("xyz", ex.getMessage()); } }
@Override public R handleExecutionException(ExecutionException ex, ParseResult parseResult) { // find top level command CommandLine cmdLine = ex.getCommandLine(); while (cmdLine.getParent() != null) { cmdLine = cmdLine.getParent(); } JetCommandLine jetCmd = cmdLine.getCommand(); if (jetCmd.isVerbose) { ex.printStackTrace(err()); } else { err().println("ERROR: " + ex.getCause().getMessage()); err().println(); err().println("To see the full stack trace, re-run with the -v/--verbose option"); } if (hasExitCode()) { exit(exitCode()); } throw ex; } }
PantheonController<?> buildController() { try { return controllerBuilder .synchronizerConfiguration(buildSyncConfig()) .homePath(dataDir()) .ethNetworkConfig(updateNetworkConfig(getNetwork())) .syncWithOttoman(false) // ottoman feature is still there but it's now removed from CLI .miningParameters( new MiningParameters(coinbase, minTransactionGasPrice, extraData, isMiningEnabled)) .devMode(NetworkName.DEV.equals(getNetwork())) .nodePrivateKeyFile(nodePrivateKeyFile()) .metricsSystem(metricsSystem) .privacyParameters(orionConfiguration()) .build(); } catch (final InvalidConfigurationException e) { throw new ExecutionException(new CommandLine(this), e.getMessage()); } catch (final IOException e) { throw new ExecutionException(new CommandLine(this), "Invalid path", e); } }
@Test public void testCallWithFactory() { Runnable[] variations = new Runnable[] { new Runnable() {public void run() {CommandLine.call(MyCallable.class, new InnerClassFactory(this), "-x", "a");}}, new Runnable() {public void run() {CommandLine.call(MyCallable.class, new InnerClassFactory(this), System.out, "-x", "a");}}, new Runnable() {public void run() {CommandLine.call(MyCallable.class, new InnerClassFactory(this), System.out, Help.Ansi.OFF, "-x", "a");}}, new Runnable() {public void run() {CommandLine.call(MyCallable.class, new InnerClassFactory(this), System.out, System.out, Help.Ansi.OFF, "-x", "a");}}, }; for (Runnable r : variations) { try { r.run(); } catch (ExecutionException ex) { assertTrue(ex.getMessage().startsWith("Error while calling command (picocli.CommandLineParseWithHandlersTest$MyCallable")); assertTrue(ex.getCause() instanceof IllegalStateException); assertEquals("this is a test", ex.getCause().getMessage()); } } }
@Test public void testRunWithFactory() { Runnable[] variations = new Runnable[] { new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), "-x", "a");}}, new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), System.out, "-x", "a");}}, new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), System.out, Help.Ansi.OFF, "-x", "a");}}, new Runnable() {public void run() {CommandLine.run(MyRunnable.class, new InnerClassFactory(this), System.out, System.out, Help.Ansi.OFF, "-x", "a");}}, }; for (Runnable r : variations) { try { r.run(); } catch (ExecutionException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Error while running command (picocli.CommandLineParseWithHandlersTest$MyRunnable")); assertTrue(ex.getCause() instanceof IllegalStateException); assertEquals("this is a test", ex.getCause().getMessage()); } } }
@SuppressWarnings("deprecation") private void verifyAllFail(Factory factory, String prefix, String suffix, String[] args) { IParseResultHandler[] handlers = new IParseResultHandler[] { new RunFirst(), new RunLast(), new RunAll() }; for (IParseResultHandler handler : handlers) { String descr = handler.getClass().getSimpleName(); try { new CommandLine(factory.create()).parseWithHandler(handler, System.out, args); fail(descr + ": expected exception"); } catch (ExecutionException ex) { String actual = ex.getMessage(); assertTrue(descr + ": " + actual, actual.startsWith(prefix)); assertTrue(descr + ": " + actual, actual.endsWith(suffix)); } } }
/** * If an Exception occurs during {@link #runRunnableSubcommand(List)}, or {@link #runScriptBody()} * then this gets called to report the problem. * The default behavior is to throw a new {@code ExecutionException} wrapping the specified exception. * * @param commandLine The CommandLine instance * @param args The argument array * @param ex The Exception that occurred * @return The value that Script.run should return when overriding this method * @throws ExecutionException wrapping the specified exception by default */ public Object handleExecutionException(CommandLine commandLine, String[] args, Exception ex) { if (ex instanceof ExecutionException) { throw (ExecutionException) ex; } throw new ExecutionException(commandLine, ex.toString(), ex); }
@Test public void testExecutionExceptionIfRunnableThrowsExecutionException() { @Command class App implements Runnable { @Spec CommandSpec spec; public void run() { throw new ExecutionException(spec.commandLine(), "abc"); } } try { CommandLine.run(new App()); } catch (ExecutionException ex) { assertEquals("abc", ex.getMessage()); } }
@Test public void testExecutionExceptionIfCallableThrowsExecutionException() { @Command class App implements Callable<Void> { @Spec CommandSpec spec; public Void call() { throw new ExecutionException(spec.commandLine(), "abc"); } } try { CommandLine.call(new App()); } catch (ExecutionException ex) { assertEquals("abc", ex.getMessage()); } }
@Test public void testHandlerThrowsExecutionException() { @Command class App { } try { new CommandLine(new App()).parseWithHandler(new IParseResultHandler2<Object>() { public Object handleParseResult(ParseResult parseResult) throws ExecutionException { throw new ExecutionException(new CommandLine(new App()), "xyz"); } }, new String[0]); } catch (ExecutionException ex) { assertEquals("xyz", ex.getMessage()); } }
public Object handleParseResult(ParseResult parseResult) throws ExecutionException { throw new ExecutionException(new CommandLine(new App()), "xyz"); } }, new String[0]);
/** Returns version information for this command, to print to the console when the user specifies an * {@linkplain OptionSpec#versionHelp() option} to request version help. This is not part of the usage help message. * @return the version strings generated by the {@link #versionProvider() version provider} if one is set, otherwise the {@linkplain #version(String...) version literals}*/ public String[] version() { if (versionProvider != null) { try { return versionProvider.getVersion(); } catch (Exception ex) { String msg = "Could not get version info from " + versionProvider + ": " + ex; throw new ExecutionException(this.commandLine, msg, ex); } } return version == null ? UsageMessageSpec.DEFAULT_MULTI_LINE : version; }
public Void handleParseResult(ParseResult parseResult) throws ExecutionException { throw new ExecutionException(new CommandLine(new App()), "xyz"); } };
@Command public static void throwsExecutionException() { throw new ExecutionException(new CommandLine(new StaticMethodCommand(8)), "abc"); }