Merge pull request #15228 from rjernst/cli_error

Cli tools: Use toString instead of getMessage for exceptions
This commit is contained in:
Ryan Ernst 2015-12-03 19:01:33 -08:00
commit b50d94efab
2 changed files with 23 additions and 2 deletions

View File

@ -116,7 +116,7 @@ public abstract class Terminal {
} }
public void printError(Throwable t) { public void printError(Throwable t) {
printError("%s", t.getMessage()); printError("%s", t.toString());
if (isDebugEnabled) { if (isDebugEnabled) {
printStackTrace(t); printStackTrace(t);
} }

View File

@ -19,6 +19,9 @@
package org.elasticsearch.common.cli; package org.elasticsearch.common.cli;
import java.nio.file.NoSuchFileException;
import java.util.List;
import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
@ -44,10 +47,28 @@ public class TerminalTests extends CliToolTestCase {
assertPrinted(terminal, Terminal.Verbosity.VERBOSE, "text"); assertPrinted(terminal, Terminal.Verbosity.VERBOSE, "text");
} }
public void testError() throws Exception {
try {
// actually throw so we have a stacktrace
throw new NoSuchFileException("/path/to/some/file");
} catch (NoSuchFileException e) {
CaptureOutputTerminal terminal = new CaptureOutputTerminal(Terminal.Verbosity.NORMAL);
terminal.printError(e);
List<String> output = terminal.getTerminalOutput();
assertFalse(output.isEmpty());
assertTrue(output.get(0), output.get(0).contains("NoSuchFileException")); // exception class
assertTrue(output.get(0), output.get(0).contains("/path/to/some/file")); // message
assertEquals(1, output.size());
// TODO: we should test stack trace is printed in debug mode...except debug is a sysprop instead of
// a command line param...maybe it should be VERBOSE instead of a separate debug prop?
}
}
private void assertPrinted(CaptureOutputTerminal logTerminal, Terminal.Verbosity verbosity, String text) { private void assertPrinted(CaptureOutputTerminal logTerminal, Terminal.Verbosity verbosity, String text) {
logTerminal.print(verbosity, text); logTerminal.print(verbosity, text);
assertThat(logTerminal.getTerminalOutput(), hasSize(1)); assertThat(logTerminal.getTerminalOutput(), hasSize(1));
assertThat(logTerminal.getTerminalOutput(), hasItem(is("text"))); assertThat(logTerminal.getTerminalOutput(), hasItem(text));
logTerminal.terminalOutput.clear(); logTerminal.terminalOutput.clear();
} }