diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-01-15 16:18:33 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-01-15 16:18:33 +0100 |
commit | 13859c479dcf2d8340eeb6a585f59d0681ab6257 (patch) | |
tree | 912586aff488bf10166a406e8638d6e5f8a55b44 /src/main/java | |
parent | 65fbc8f07602ab84a1201d608bbe7193c544cf40 (diff) | |
download | sonar-scanner-cli-13859c479dcf2d8340eeb6a585f59d0681ab6257.tar.gz sonar-scanner-cli-13859c479dcf2d8340eeb6a585f59d0681ab6257.zip |
SONARPLUGINS-2604 Improve error reporting and introduce -e flag
* Try to make errors more readable for non java expert
* Mimic what is done in Maven CLI
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/org/sonar/runner/Logs.java | 9 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/Main.java | 69 | ||||
-rw-r--r-- | src/main/java/org/sonar/runner/Runner.java | 4 |
3 files changed, 68 insertions, 14 deletions
diff --git a/src/main/java/org/sonar/runner/Logs.java b/src/main/java/org/sonar/runner/Logs.java index 3ce1cca..2bd24d6 100644 --- a/src/main/java/org/sonar/runner/Logs.java +++ b/src/main/java/org/sonar/runner/Logs.java @@ -24,10 +24,17 @@ final class Logs { } static void info(String message) { - System.out.println(message); + System.out.println("INFO: " + message); } static void error(String message) { System.err.println("ERROR: " + message); } + + static void error(String message, Throwable t) { + System.err.println("ERROR: " + message); + if (t != null) { + t.printStackTrace(System.err); + } + } } diff --git a/src/main/java/org/sonar/runner/Main.java b/src/main/java/org/sonar/runner/Main.java index 57e9a32..4d73b35 100644 --- a/src/main/java/org/sonar/runner/Main.java +++ b/src/main/java/org/sonar/runner/Main.java @@ -49,6 +49,7 @@ public final class Main { private boolean debugMode = false; private boolean displayVersionOnly = false; + private boolean displayStackTrace = false; private String command; @VisibleForTesting Properties globalProperties; @@ -68,16 +69,18 @@ public final class Main { private void execute(String[] args) { Properties argsProperties = parseArguments(args); - Logs.info("Runner version: " + Version.getVersion()); - Logs.info("Java version: " + System.getProperty("java.version", "<unknown>") + System.out.println("Runner version: " + Version.getVersion()); + System.out.println("Java version: " + System.getProperty("java.version", "<unknown>") + ", vendor: " + System.getProperty("java.vendor", "<unknown>")); - Logs.info("OS name: \"" + System.getProperty("os.name") + "\", version: \"" + System.getProperty("os.version") + "\", arch: \"" + System.getProperty("os.arch") + "\""); + System.out + .println("OS name: \"" + System.getProperty("os.name") + "\", version: \"" + System.getProperty("os.version") + "\", arch: \"" + System.getProperty("os.arch") + "\""); if (!displayVersionOnly) { - execute(argsProperties); + int result = execute(argsProperties); + System.exit(result); } } - private void execute(Properties argsProperties) { + private int execute(Properties argsProperties) { Stats stats = new Stats().start(); try { loadProperties(argsProperties); @@ -92,12 +95,52 @@ public final class Main { try { Logs.info("Work directory: " + runner.getWorkDir().getCanonicalPath()); } catch (IOException e) { - throw new RunnerException(e); + throw new RunnerException("Unable to display work directory", e); } runner.execute(); - } finally { - stats.stop(); + } catch (Exception e) { + displayExecutionResult(stats, "FAILURE"); + showError("Error during Sonar runner execution", e, displayStackTrace); + return 1; } + displayExecutionResult(stats, "SUCCESS"); + return 0; + } + + private void displayExecutionResult(Stats stats, String resultMsg) { + Logs.info("------------------------------------------------------------------------"); + Logs.info("EXECUTION " + resultMsg); + Logs.info("------------------------------------------------------------------------"); + stats.stop(); + Logs.info("------------------------------------------------------------------------"); + } + + public void showError(String message, Throwable e, boolean showStackTrace) { + if (showStackTrace) { + Logs.error(message, e); + if (!debugMode) { + Logs.error(""); + suggestDebugMode(); + } + } + else { + Logs.error(message); + if (e != null) { + Logs.error(e.getMessage()); + for (Throwable cause = e.getCause(); cause != null; cause = cause.getCause()) { + Logs.error("Caused by: " + cause.getMessage()); + } + } + Logs.error(""); + Logs.error("To see the full stack trace of the errors, re-run Sonar Runner with the -e switch."); + if (!debugMode) { + suggestDebugMode(); + } + } + } + + private void suggestDebugMode() { + Logs.error("Re-run Sonar Runner using the -X switch to enable full debug logging."); } @VisibleForTesting @@ -211,6 +254,10 @@ public final class Main { else if ("-v".equals(arg) || "--version".equals(arg)) { displayVersionOnly = true; } + else if ("-e".equals(arg) || "--errors".equals(arg)) { + displayStackTrace = true; + Logs.info("Error stacktraces are turned on."); + } else if ("-X".equals(arg) || "--debug".equals(arg)) { props.setProperty(Runner.PROPERTY_VERBOSE, "true"); debugMode = true; @@ -250,8 +297,7 @@ public final class Main { } private void printError(String message) { - Logs.info(""); - Logs.info(message); + Logs.error(message); printUsage(); } @@ -263,10 +309,11 @@ public final class Main { Logs.info(" analyse-project Run Sonar analysis task on the current project (default)"); Logs.info(" list-tasks Display all tasks available"); Logs.info("Options:"); + Logs.info(" -D,--define <arg> Define property"); + Logs.info(" -e,--errors Produce execution error messages"); Logs.info(" -h,--help Display help information"); Logs.info(" -v,--version Display version information"); Logs.info(" -X,--debug Produce execution debug output"); - Logs.info(" -D,--define <arg> Define property"); System.exit(0); } } diff --git a/src/main/java/org/sonar/runner/Runner.java b/src/main/java/org/sonar/runner/Runner.java index ecddd0c..dd53995 100644 --- a/src/main/java/org/sonar/runner/Runner.java +++ b/src/main/java/org/sonar/runner/Runner.java @@ -315,10 +315,10 @@ public final class Runner { method.invoke(launcher); } catch (InvocationTargetException e) { // Unwrap original exception - throw new RunnerException(e.getTargetException()); + throw new RunnerException("Unable to execute Sonar", e.getTargetException()); } catch (Exception e) { // Catch all other exceptions, which relates to reflection - throw new RunnerException(e); + throw new RunnerException("Unable to execute Sonar", e); } finally { Thread.currentThread().setContextClassLoader(oldContextClassLoader); } |