Skip to content

Commit

Permalink
SCANJLIB-169 Omit the stack trace of connection errors with SonarQube
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-garcia-sonarsource committed Dec 10, 2024
1 parent 2bebfff commit ebfdc45
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

public class ScannerException extends RuntimeException {

public ScannerException(String message) {
super(message);
}

public ScannerException(String message, Throwable cause) {
super(message, cause);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -46,10 +48,19 @@ public void setUp() {

@Test
void should_use_isolated_classloader() {
var rules = new ClassloadRules(new HashSet<String>(), new HashSet<String>());
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 {
Expand Down

0 comments on commit ebfdc45

Please sign in to comment.