diff --git a/lib/src/main/java/org/sonarsource/scanner/lib/internal/IsolatedLauncherFactory.java b/lib/src/main/java/org/sonarsource/scanner/lib/internal/IsolatedLauncherFactory.java index e09f5d96..b4aea3a7 100644 --- a/lib/src/main/java/org/sonarsource/scanner/lib/internal/IsolatedLauncherFactory.java +++ b/lib/src/main/java/org/sonarsource/scanner/lib/internal/IsolatedLauncherFactory.java @@ -24,6 +24,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import javax.annotation.Nullable; @@ -78,6 +79,8 @@ IsolatedLauncherAndClassloader createLauncher(final LegacyScannerEngineDownloade tempCleaning.clean(); return new IsolatedLauncherAndClassloader(objProxy, cl, jarFiles.stream().allMatch(CachedFile::isCacheHit)); + } catch (IllegalStateException e) { + throw new ScannerException("Unable to execute SonarScanner analysis: " + Optional.ofNullable(e.getCause()).map(Throwable::getMessage).orElse(e.getMessage())); } catch (Exception e) { // Catch all other exceptions, which relates to reflection throw new ScannerException("Unable to execute SonarScanner analysis", e); diff --git a/lib/src/main/java/org/sonarsource/scanner/lib/internal/ScannerException.java b/lib/src/main/java/org/sonarsource/scanner/lib/internal/ScannerException.java index a5300075..097958f0 100644 --- a/lib/src/main/java/org/sonarsource/scanner/lib/internal/ScannerException.java +++ b/lib/src/main/java/org/sonarsource/scanner/lib/internal/ScannerException.java @@ -21,6 +21,10 @@ public class ScannerException extends RuntimeException { + public ScannerException(String message) { + super(message); + } + public ScannerException(String message, Throwable cause) { super(message, cause); } diff --git a/lib/src/test/java/org/sonarsource/scanner/lib/internal/IsolatedLauncherFactoryTest.java b/lib/src/test/java/org/sonarsource/scanner/lib/internal/IsolatedLauncherFactoryTest.java index 1fb478f3..d0edc101 100644 --- a/lib/src/test/java/org/sonarsource/scanner/lib/internal/IsolatedLauncherFactoryTest.java +++ b/lib/src/test/java/org/sonarsource/scanner/lib/internal/IsolatedLauncherFactoryTest.java @@ -19,6 +19,7 @@ */ package org.sonarsource.scanner.lib.internal; +import java.net.ConnectException; import java.util.HashSet; import java.util.Map; import java.util.Properties; @@ -27,8 +28,9 @@ import org.sonarsource.scanner.lib.internal.batch.IsolatedLauncher; import org.sonarsource.scanner.lib.internal.batch.LogOutput; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class IsolatedLauncherFactoryTest { IsolatedLauncherFactory factory; @@ -37,7 +39,7 @@ class IsolatedLauncherFactoryTest { LegacyScannerEngineDownloader legacyScannerEngineDownloader; @BeforeEach - public void setUp() { + void setUp() { tempCleaning = mock(TempCleaning.class); factory = new IsolatedLauncherFactory(FakeIsolatedLauncher.class.getName(), tempCleaning); props = new Properties(); @@ -46,10 +48,19 @@ public void setUp() { @Test void should_use_isolated_classloader() { - var rules = new ClassloadRules(new HashSet(), new HashSet()); - assertThrows(ScannerException.class, () -> { - factory.createLauncher(legacyScannerEngineDownloader, rules); - }); + var rules = new ClassloadRules(new HashSet<>(), new HashSet<>()); + assertThatThrownBy(() -> factory.createLauncher(legacyScannerEngineDownloader, rules)) + .isInstanceOf(ScannerException.class); + } + + @Test + void should_omit_connection_error_exceptions_and_return_error_message() { + when(legacyScannerEngineDownloader.getOrDownload()).thenThrow( + new IllegalStateException("Fail to get bootstrap index from server", new ConnectException("Failed to connect to localhost/127.0.0.1:9000")) + ); + assertThatThrownBy(() -> factory.createLauncher(legacyScannerEngineDownloader, new ClassloadRules(new HashSet<>(), new HashSet<>()))) + .isInstanceOf(ScannerException.class) + .hasMessage("Unable to execute SonarScanner analysis: Failed to connect to localhost/127.0.0.1:9000"); } public static class FakeIsolatedLauncher implements IsolatedLauncher {