Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCANJLIB-169 Omit the stack trace of connection errors with SonarQube #214

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading