Skip to content

Commit 427e06e

Browse files
committed
SCANJLIB-231 Preserve the original exception as suppressed
1 parent b00d9ca commit 427e06e

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

lib/src/main/java/org/sonarsource/scanner/lib/ScannerEngineBootstrapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ private static String getServerVersion(ServerConnection serverConnection, boolea
145145
try {
146146
return serverConnection.callWebApi("/api/server/version");
147147
} catch (Exception e2) {
148-
throw new IllegalStateException("Failed to get server version", e);
148+
var ex = new IllegalStateException("Failed to get server version", e2);
149+
ex.addSuppressed(e);
150+
throw ex;
149151
}
150152
}
151153
}

lib/src/test/java/org/sonarsource/scanner/lib/ScannerEngineBootstrapperTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.sonarsource.scanner.lib.internal.http.ServerConnection;
3838

3939
import static org.assertj.core.api.Assertions.assertThat;
40+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4041
import static org.assertj.core.api.Assertions.entry;
4142
import static org.junit.jupiter.api.Assertions.assertThrows;
4243
import static org.mockito.ArgumentMatchers.any;
@@ -107,7 +108,8 @@ void should_use_old_bootstrapping_with_sonarqube_10_5() throws Exception {
107108

108109
ScannerEngineBootstrapper bootstrapper = new ScannerEngineBootstrapper("Gradle", "3.1", system, serverConnection,
109110
launcherFactory, scannerEngineLauncherFactory);
110-
when(serverConnection.callRestApi("/analysis/version")).thenReturn("10.5");
111+
when(serverConnection.callRestApi("/analysis/version")).thenThrow(new IOException("404 Not found"));
112+
when(serverConnection.callWebApi("/api/server/version")).thenReturn("10.5");
111113

112114
try (var scannerEngineFacade = bootstrapper.setBootstrapProperty(ScannerProperties.HOST_URL, "http://localhost").bootstrap()) {
113115
verify(launcherFactory).createLauncher(eq(serverConnection), any(FileCache.class));
@@ -116,6 +118,26 @@ void should_use_old_bootstrapping_with_sonarqube_10_5() throws Exception {
116118
}
117119
}
118120

121+
@Test
122+
void should_preserve_both_exceptions_when_checking_version() throws Exception {
123+
IsolatedLauncherFactory launcherFactory = mock(IsolatedLauncherFactory.class);
124+
when(launcherFactory.createLauncher(eq(serverConnection), any(FileCache.class)))
125+
.thenReturn(mock(IsolatedLauncherFactory.IsolatedLauncherAndClassloader.class));
126+
127+
ScannerEngineBootstrapper bootstrapper = new ScannerEngineBootstrapper("Gradle", "3.1", system, serverConnection,
128+
launcherFactory, scannerEngineLauncherFactory);
129+
when(serverConnection.callRestApi("/analysis/version")).thenThrow(new IOException("404 Not found"));
130+
when(serverConnection.callWebApi("/api/server/version")).thenThrow(new IOException("400 Server Error"));
131+
132+
assertThatThrownBy(() -> {
133+
try (var ignored = bootstrapper.setBootstrapProperty(ScannerProperties.HOST_URL, "http://localhost").bootstrap()) {
134+
// Should throw
135+
}
136+
})
137+
.hasMessage("Failed to get server version")
138+
.hasStackTraceContaining("400 Server Error", "404 Not found");
139+
}
140+
119141
@Test
120142
void should_launch_in_simulation_mode() throws Exception {
121143
try (var scannerEngine = underTest

0 commit comments

Comments
 (0)