From 00e8ca4b08e25f0db409d90a56113a0d0907fdf0 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 16 Sep 2016 15:21:00 +0200 Subject: [PATCH] SQSCANNER-23 Support the new 'MessageException' unchecked exception and log by default the error stack trace only when a non-MessageException is thrown --- .../org/sonarsource/scanner/cli/Main.java | 24 +++++++++---------- .../MessageException.java | 2 +- .../org/sonarsource/scanner/cli/MainTest.java | 6 ++--- 3 files changed, 15 insertions(+), 17 deletions(-) rename src/test/java/org/sonar/api/utils/{MessageException => }/MessageException.java (95%) diff --git a/src/main/java/org/sonarsource/scanner/cli/Main.java b/src/main/java/org/sonarsource/scanner/cli/Main.java index b8b6e68..83bdf33 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Main.java +++ b/src/main/java/org/sonarsource/scanner/cli/Main.java @@ -81,7 +81,7 @@ public class Main { runner.stop(); exit.exit(Exit.SUCCESS); } - + private void checkSkip(Properties properties) { if ("true".equalsIgnoreCase(properties.getProperty(ScanProperties.SKIP))) { logger.info("SonarQube Scanner analysis skipped"); @@ -125,27 +125,25 @@ public class Main { logger.error(message, e); } else { logger.error(message); - if (e != null) { - logger.error(e.getMessage()); - String previousMsg = ""; - for (Throwable cause = e.getCause(); cause != null - && cause.getMessage() != null - && !cause.getMessage().equals(previousMsg); cause = cause.getCause()) { - logger.error("Caused by: " + cause.getMessage()); - previousMsg = cause.getMessage(); - } + logger.error(e.getMessage()); + String previousMsg = ""; + for (Throwable cause = e.getCause(); cause != null + && cause.getMessage() != null + && !cause.getMessage().equals(previousMsg); cause = cause.getCause()) { + logger.error("Caused by: " + cause.getMessage()); + previousMsg = cause.getMessage(); } } - + if (!cli.isDebugEnabled()) { logger.error(""); suggestDebugMode(); } } - + private static boolean showStackTrace(Throwable e, boolean debug) { // class not available at compile time (loaded by isolated classloader) - return debug || "org.sonar.api.utils.MessageException".equals(e.getClass().getName()); + return debug || !"org.sonar.api.utils.MessageException".equals(e.getClass().getName()); } private void suggestDebugMode() { diff --git a/src/test/java/org/sonar/api/utils/MessageException/MessageException.java b/src/test/java/org/sonar/api/utils/MessageException.java similarity index 95% rename from src/test/java/org/sonar/api/utils/MessageException/MessageException.java rename to src/test/java/org/sonar/api/utils/MessageException.java index 163fc0f..0e72d3c 100644 --- a/src/test/java/org/sonar/api/utils/MessageException/MessageException.java +++ b/src/test/java/org/sonar/api/utils/MessageException.java @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.api.utils.MessageException; +package org.sonar.api.utils; public class MessageException extends RuntimeException { public MessageException(String msg) { diff --git a/src/test/java/org/sonarsource/scanner/cli/MainTest.java b/src/test/java/org/sonarsource/scanner/cli/MainTest.java index abf5fc0..7efc17a 100644 --- a/src/test/java/org/sonarsource/scanner/cli/MainTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/MainTest.java @@ -28,7 +28,7 @@ import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.sonar.api.utils.MessageException.MessageException; +import org.sonar.api.utils.MessageException; import org.sonarsource.scanner.api.EmbeddedScanner; import org.sonarsource.scanner.api.ScanProperties; @@ -91,7 +91,7 @@ public class MainTest { verify(runner).stop(); verify(exit).exit(Exit.ERROR); - verify(logs).error("Caused by: NPE"); + verify(logs).error("Error during SonarQube Scanner execution", e); } @Test @@ -99,7 +99,7 @@ public class MainTest { Exception e = createException(false); testException(e, false); - verify(logs).error("Error during SonarQube Scanner execution"); + verify(logs).error("Error during SonarQube Scanner execution", e); verify(logs).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging."); } -- 2.39.5