From: Duarte Meneses Date: Fri, 16 Sep 2016 13:21:00 +0000 (+0200) Subject: SQSCANNER-23 Support the new 'MessageException' unchecked exception and log by defaul... X-Git-Tag: 2.8~5 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=00e8ca4b08e25f0db409d90a56113a0d0907fdf0;p=sonar-scanner-cli.git SQSCANNER-23 Support the new 'MessageException' unchecked exception and log by default the error stack trace only when a non-MessageException is thrown --- 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.java b/src/test/java/org/sonar/api/utils/MessageException.java new file mode 100644 index 0000000..0e72d3c --- /dev/null +++ b/src/test/java/org/sonar/api/utils/MessageException.java @@ -0,0 +1,26 @@ +/* + * SonarQube Scanner + * Copyright (C) 2011-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * 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; + +public class MessageException extends RuntimeException { + public MessageException(String msg) { + super(msg); + } +} diff --git a/src/test/java/org/sonar/api/utils/MessageException/MessageException.java b/src/test/java/org/sonar/api/utils/MessageException/MessageException.java deleted file mode 100644 index 163fc0f..0000000 --- a/src/test/java/org/sonar/api/utils/MessageException/MessageException.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * SonarQube Scanner - * Copyright (C) 2011-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * 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; - -public class MessageException extends RuntimeException { - public MessageException(String msg) { - super(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."); }