aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2019-03-15 14:31:27 +0100
committerSimon Brandhof <simon.brandhof@sonarsource.com>2019-03-15 21:19:59 +0100
commit03a5ab2517615d93d071aca18a3900f44b3b7dab (patch)
tree92b2be7d90d75ee4067873706368893df9fe6fb4
parentfe6edeb839d98c4c8a5b4bf66fb92349fc9e58fa (diff)
downloadsonar-scanner-cli-03a5ab2517615d93d071aca18a3900f44b3b7dab.tar.gz
sonar-scanner-cli-03a5ab2517615d93d071aca18a3900f44b3b7dab.zip
SQSCANNER-58 Return a different error code for functional errors
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Cli.java2
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Exit.java3
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Main.java9
-rw-r--r--src/test/java/org/sonarsource/scanner/cli/CliTest.java4
-rw-r--r--src/test/java/org/sonarsource/scanner/cli/MainTest.java18
5 files changed, 19 insertions, 17 deletions
diff --git a/src/main/java/org/sonarsource/scanner/cli/Cli.java b/src/main/java/org/sonarsource/scanner/cli/Cli.java
index 66196a7..9ffc5dd 100644
--- a/src/main/java/org/sonarsource/scanner/cli/Cli.java
+++ b/src/main/java/org/sonarsource/scanner/cli/Cli.java
@@ -135,7 +135,7 @@ class Cli {
private void printErrorAndExit(String message) {
logger.error(message);
printUsage();
- exit.exit(Exit.ERROR);
+ exit.exit(Exit.INTERNAL_ERROR);
}
private void printUsage() {
diff --git a/src/main/java/org/sonarsource/scanner/cli/Exit.java b/src/main/java/org/sonarsource/scanner/cli/Exit.java
index adae579..69e9c28 100644
--- a/src/main/java/org/sonarsource/scanner/cli/Exit.java
+++ b/src/main/java/org/sonarsource/scanner/cli/Exit.java
@@ -21,7 +21,8 @@ package org.sonarsource.scanner.cli;
class Exit {
static final int SUCCESS = 0;
- static final int ERROR = 1;
+ static final int INTERNAL_ERROR = 1;
+ static final int USER_ERROR = 2;
void exit(int status) {
System.exit(status);
diff --git a/src/main/java/org/sonarsource/scanner/cli/Main.java b/src/main/java/org/sonarsource/scanner/cli/Main.java
index e6f913a..3e74b17 100644
--- a/src/main/java/org/sonarsource/scanner/cli/Main.java
+++ b/src/main/java/org/sonarsource/scanner/cli/Main.java
@@ -64,7 +64,7 @@ public class Main {
void execute() {
Stats stats = new Stats(logger).start();
- int status = Exit.ERROR;
+ int status = Exit.INTERNAL_ERROR;
try {
Properties p = conf.properties();
checkSkip(p);
@@ -77,6 +77,7 @@ public class Main {
} catch (Throwable e) {
displayExecutionResult(stats, "FAILURE");
showError("Error during SonarQube Scanner execution", e, cli.isDebugEnabled());
+ status = isUserError(e) ? Exit.USER_ERROR : Exit.INTERNAL_ERROR;
} finally {
exit.exit(status);
}
@@ -121,7 +122,7 @@ public class Main {
}
private void showError(String message, Throwable e, boolean debug) {
- if (showStackTrace(e, debug)) {
+ if (debug && !isUserError(e)) {
logger.error(message, e);
} else {
logger.error(message);
@@ -141,9 +142,9 @@ public class Main {
}
}
- private static boolean showStackTrace(Throwable e, boolean debug) {
+ private static boolean isUserError(Throwable e) {
// class not available at compile time (loaded by isolated classloader)
- return debug && !"org.sonar.api.utils.MessageException".equals(e.getClass().getName());
+ return "org.sonar.api.utils.MessageException".equals(e.getClass().getName());
}
private void suggestDebugMode() {
diff --git a/src/test/java/org/sonarsource/scanner/cli/CliTest.java b/src/test/java/org/sonarsource/scanner/cli/CliTest.java
index 8072be5..c843ff6 100644
--- a/src/test/java/org/sonarsource/scanner/cli/CliTest.java
+++ b/src/test/java/org/sonarsource/scanner/cli/CliTest.java
@@ -53,7 +53,7 @@ public class CliTest {
cli = new Cli(exit, logs);
cli.parse(new String[] {"-D"});
verify(logs).error("Missing argument for option -D/--define");
- verify(exit).exit(Exit.ERROR);
+ verify(exit).exit(Exit.INTERNAL_ERROR);
}
@Test
@@ -142,7 +142,7 @@ public class CliTest {
cli.parse(new String[] {"-w"});
verify(logs).error("Unrecognized option: -w");
verify(logs).info("usage: sonar-scanner [options]");
- verify(exit).exit(Exit.ERROR);
+ verify(exit).exit(Exit.INTERNAL_ERROR);
}
@Test
diff --git a/src/test/java/org/sonarsource/scanner/cli/MainTest.java b/src/test/java/org/sonarsource/scanner/cli/MainTest.java
index e8f19a6..7090196 100644
--- a/src/test/java/org/sonarsource/scanner/cli/MainTest.java
+++ b/src/test/java/org/sonarsource/scanner/cli/MainTest.java
@@ -88,7 +88,7 @@ public class MainTest {
Main main = new Main(exit, cli, conf, scannerFactory, logs);
main.execute();
- verify(exit).exit(Exit.ERROR);
+ verify(exit).exit(Exit.INTERNAL_ERROR);
verify(logs).error("Error during SonarQube Scanner execution", e);
}
@@ -106,14 +106,14 @@ public class MainTest {
verify(runner).start();
verify(runner, never()).execute(any());
- verify(exit).exit(Exit.ERROR);
+ verify(exit).exit(Exit.INTERNAL_ERROR);
verify(logs).error("Error during SonarQube Scanner execution", e);
}
@Test
public void show_error_MessageException() {
Exception e = createException(true);
- testException(e, false, false);
+ testException(e, false, false, Exit.USER_ERROR);
verify(logs).error("Error during SonarQube Scanner execution");
verify(logs).error("Caused by: NPE");
@@ -123,7 +123,7 @@ public class MainTest {
@Test
public void show_error_MessageException_embedded() {
Exception e = createException(true);
- testException(e, false, true);
+ testException(e, false, true, Exit.USER_ERROR);
verify(logs).error("Error during SonarQube Scanner execution");
verify(logs).error("Caused by: NPE");
@@ -132,7 +132,7 @@ public class MainTest {
@Test
public void show_error_MessageException_debug() {
Exception e = createException(true);
- testException(e, true, false);
+ testException(e, true, false, Exit.USER_ERROR);
verify(logs).error("Error during SonarQube Scanner execution");
verify(logs).error("my message");
@@ -142,7 +142,7 @@ public class MainTest {
@Test
public void show_error_MessageException_debug_embedded() {
Exception e = createException(true);
- testException(e, true, true);
+ testException(e, true, true, Exit.USER_ERROR);
verify(logs).error("Error during SonarQube Scanner execution");
verify(logs).error("my message");
@@ -152,13 +152,13 @@ public class MainTest {
@Test
public void show_error_debug() {
Exception e = createException(false);
- testException(e, true, false);
+ testException(e, true, false, Exit.INTERNAL_ERROR);
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 void testException(Exception e, boolean debugEnabled, boolean isEmbedded) {
+ private void testException(Exception e, boolean debugEnabled, boolean isEmbedded, int expectedExitCode) {
when(cli.isDebugEnabled()).thenReturn(debugEnabled);
when(cli.isEmbedded()).thenReturn(isEmbedded);
@@ -169,7 +169,7 @@ public class MainTest {
Main main = new Main(exit, cli, conf, scannerFactory, logs);
main.execute();
- verify(exit).exit(Exit.ERROR);
+ verify(exit).exit(expectedExitCode);
}
private Exception createException(boolean messageException) {