private boolean debugEnabled = false;
private boolean displayVersionOnly = false;
- private boolean displayStackTrace = false;
private final Properties props = new Properties();
private final Exit exit;
private final Logs logger;
return displayVersionOnly;
}
- boolean isDisplayStackTrace() {
- return displayStackTrace;
- }
-
Properties properties() {
return props;
}
} else if ("-v".equals(arg) || "--version".equals(arg)) {
displayVersionOnly = true;
-
+
} else if ("-e".equals(arg) || "--errors".equals(arg)) {
- displayStackTrace = true;
- logger.setDisplayStackTrace(true);
+ logger.info("Option -e/--errors is no longer supported and will be ignored");
} else if ("-X".equals(arg) || "--debug".equals(arg)) {
props.setProperty("sonar.verbose", "true");
private void reset() {
props.clear();
debugEnabled = false;
- displayStackTrace = false;
displayVersionOnly = false;
}
logger.info("");
logger.info("Options:");
logger.info(" -D,--define <arg> Define property");
- logger.info(" -e,--errors Produce execution error messages");
logger.info(" -h,--help Display help information");
logger.info(" -v,--version Display version information");
logger.info(" -X,--debug Produce execution debug output");
runAnalysis(stats, p);
} catch (Exception e) {
displayExecutionResult(stats, "FAILURE");
- showError("Error during SonarQube Scanner execution", e, cli.isDisplayStackTrace() || cli.isDebugEnabled());
+ showError("Error during SonarQube Scanner execution", e, cli.isDebugEnabled());
exit.exit(Exit.ERROR);
}
exit.exit(Exit.SUCCESS);
}
- if (cli.isDisplayStackTrace()) {
- logger.info("Error stacktraces are turned on.");
- }
-
runner = runnerFactory.create(p);
}
logger.setDebugEnabled(true);
logger.setDisplayStackTrace(true);
}
-
- if (cli.isDisplayStackTrace()) {
- logger.setDisplayStackTrace(true);
- }
}
private void runAnalysis(Stats stats, Properties p) {
logger.info(SEPARATOR);
}
- private void showError(String message, Throwable e, boolean showStackTrace) {
- if (showStackTrace) {
+ private void showError(String message, Throwable e, boolean debug) {
+ if (showStackTrace(e, debug)) {
logger.error(message, e);
- if (!cli.isDebugEnabled()) {
- logger.error("");
- suggestDebugMode();
- }
} else {
logger.error(message);
if (e != null) {
previousMsg = cause.getMessage();
}
}
+ }
+
+ if (!cli.isDebugEnabled()) {
logger.error("");
- logger.error("To see the full stack trace of the errors, re-run SonarQube Scanner with the -e switch.");
- if (!cli.isDebugEnabled()) {
- suggestDebugMode();
- }
+ 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());
+ }
private void suggestDebugMode() {
logger.error("Re-run SonarQube Scanner using the -X switch to enable full debug logging.");
--- /dev/null
+/*
+ * 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);
+ }
+}
cli.parse(new String[0]);
assertThat(cli.properties()).isNotEmpty();
assertThat(cli.isDebugEnabled()).isFalse();
- assertThat(cli.isDisplayStackTrace()).isFalse();
assertThat(cli.isDisplayVersionOnly()).isFalse();
}
assertThat(cli.properties().get("boolean")).isEqualTo("true");
}
+ @Test
+ public void should_not_fail_with_errors_option() {
+ cli.parse(new String[] {"-e"});
+ }
+
@Test
public void should_parse_optional_task() {
cli.parse(new String[] {"-D", "foo=bar"});
public void should_enable_stacktrace_log() {
cli.parse(new String[] {"-e"});
assertThat(cli.isDebugEnabled()).isFalse();
- assertThat(cli.isDisplayStackTrace()).isTrue();
assertThat(cli.properties().get("sonar.verbose")).isNull();
}
public void should_disable_debug_mode_and_stacktrace_log_by_default() {
cli.parse(new String[0]);
assertThat(cli.isDebugEnabled()).isFalse();
- assertThat(cli.isDisplayStackTrace()).isFalse();
assertThat(cli.properties().get("sonar.verbose")).isNull();
}
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
+import org.sonar.api.utils.MessageException.MessageException;
import org.sonarsource.scanner.api.EmbeddedScanner;
import org.sonarsource.scanner.api.ScanProperties;
}
@Test
- public void show_error_stacktrace() {
- Exception e = show_error(false, true);
- verify(logs).error("Error during SonarQube Scanner execution", e);
+ public void show_error() {
+ Exception e = createException(false, false);
+ testException(e, false);
+
+ verify(logs).error("Error during SonarQube Scanner execution");
verify(logs).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging.");
}
@Test
- public void show_error_debug() {
- Exception e = show_error(true, false);
+ public void show_error_MessageException() {
+ Exception e = createException(false, true);
+ testException(e, false);
- verify(logs).error("Error during SonarQube Scanner execution", e);
- verify(logs, never()).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging.");
+ verify(logs).error("Error during SonarQube Scanner execution");
+ verify(logs).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging.");
}
@Test
- public void show_error_debug_stack() {
- Exception e = show_error(true, true);
+ public void show_error_debug() {
+ Exception e = createException(true, false);
+ testException(e, true);
verify(logs).error("Error during SonarQube Scanner execution", e);
verify(logs, never()).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging.");
}
- private Exception show_error(boolean debugEnabled, boolean stackTraceEnabled) {
- Exception e = new NullPointerException("NPE");
- e = new IllegalStateException("Error", e);
+ private void testException(Exception e, boolean debugEnabled) {
when(cli.isDebugEnabled()).thenReturn(debugEnabled);
- when(cli.isDisplayStackTrace()).thenReturn(stackTraceEnabled);
EmbeddedScanner runner = mock(EmbeddedScanner.class);
doThrow(e).when(runner).runAnalysis(any(Properties.class));
verify(runner).stop();
verify(exit).exit(Exit.ERROR);
+ }
+
+ private Exception createException(boolean debugEnabled, boolean messageException) {
+ Exception e;
+ if (messageException) {
+ e = new MessageException("my message");
+ } else {
+ e = new IllegalStateException("Error", new NullPointerException("NPE"));
+ }
+
return e;
}
inOrder.verify(runnerFactory, times(1)).create(p);
inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
}
-
+
@Test
public void should_skip() throws IOException {
Properties p = new Properties();