aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/main/java/org/sonar
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2024-05-07 12:39:16 +0200
committersonartech <sonartech@sonarsource.com>2024-05-13 20:02:41 +0000
commit792b34433b4a7f98c9ae1749378e68e426343728 (patch)
tree7443b028600711530e56de9a2386f526977590b6 /sonar-scanner-engine/src/main/java/org/sonar
parent3356fc08604e567bda8d5646af64099df7765b5a (diff)
downloadsonarqube-792b34433b4a7f98c9ae1749378e68e426343728.tar.gz
sonarqube-792b34433b4a7f98c9ae1749378e68e426343728.zip
Unwrap MessageException
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org/sonar')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerMain.java55
1 files changed, 42 insertions, 13 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerMain.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerMain.java
index 044b42fd620..1dbafef4ce9 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerMain.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerMain.java
@@ -30,9 +30,11 @@ import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import javax.annotation.CheckForNull;
import org.jetbrains.annotations.NotNull;
import org.slf4j.LoggerFactory;
+import org.sonar.api.utils.MessageException;
import org.sonar.batch.bootstrapper.EnvironmentInformation;
import org.sonar.batch.bootstrapper.LoggingConfiguration;
@@ -47,26 +49,53 @@ public class ScannerMain {
private static final String SCANNER_APP_VERSION_KEY = "sonar.scanner.appVersion";
public static void main(String... args) {
- try {
- run(System.in);
- } catch (Exception e) {
- LOG.error("Error during SonarScanner Engine execution", e);
- System.exit(1);
- }
+ System.exit(run(System.in));
}
- public static void run(InputStream in) {
- LOG.info("Starting SonarScanner Engine...");
+ public static int run(InputStream in) {
+ try {
+ LOG.info("Starting SonarScanner Engine...");
+
+ var properties = parseInputProperties(in);
+
+ EnvironmentConfig.processEnvVariables(properties);
- var properties = parseInputProperties(in);
+ configureLogLevel(properties);
- EnvironmentConfig.processEnvVariables(properties);
+ runScannerEngine(properties);
- configureLogLevel(properties);
+ LOG.info("SonarScanner Engine completed successfully");
+ return 0;
+ } catch (Exception e) {
+ handleException(e);
+ return 1;
+ }
+ }
+
+ private static void handleException(Exception e) {
+ var messageException = unwrapMessageException(e);
+ if (messageException.isPresent()) {
+ // Don't show the stacktrace for a message exception to not pollute the logs
+ if (LoggerFactory.getLogger(ScannerMain.class).isDebugEnabled()) {
+ LOG.error(messageException.get(), e);
+ } else {
+ LOG.error(messageException.get());
+ }
+ } else {
+ LOG.error("Error during SonarScanner Engine execution", e);
+ }
+ }
- runScannerEngine(properties);
+ private static Optional<String> unwrapMessageException(Exception t) {
+ Throwable y = t;
+ do {
+ if (y instanceof MessageException messageException) {
+ return Optional.of(messageException.getMessage());
+ }
+ y = y.getCause();
+ } while (y != null);
- LOG.info("SonarScanner Engine completed successfully");
+ return Optional.empty();
}
private static @NotNull Map<String, String> parseInputProperties(InputStream in) {