From a5ea4ea1e26eb96d9d8901567b669b2351e14128 Mon Sep 17 00:00:00 2001 From: Antoine Vigneau Date: Thu, 19 Oct 2023 16:07:56 +0200 Subject: [PATCH] SONAR-20699 Propagate meaningful exceptions to the Scanner --- .../org/sonar/server/ce/ws/SubmitAction.java | 9 +++++++++ .../sonar/server/ce/ws/SubmitActionTest.java | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/SubmitAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/SubmitAction.java index 3f9fec277e6..1e6c9e2acc9 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/SubmitAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/SubmitAction.java @@ -24,15 +24,19 @@ import java.io.InputStream; import java.util.LinkedHashMap; import java.util.Map; import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.ce.task.CeTask; import org.sonar.db.ce.CeTaskCharacteristicDto; import org.sonar.server.ce.queue.ReportSubmitter; +import org.sonar.server.exceptions.ServerException; import org.sonar.server.ws.WsUtils; import org.sonarqube.ws.Ce; +import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR; import static org.apache.commons.lang.StringUtils.abbreviate; import static org.apache.commons.lang.StringUtils.defaultIfBlank; import static org.sonar.core.component.ComponentKeys.MAX_COMPONENT_KEY_LENGTH; @@ -41,6 +45,8 @@ import static org.sonar.server.exceptions.BadRequestException.checkRequest; public class SubmitAction implements CeWsAction { + private static final Logger LOGGER = LoggerFactory.getLogger(SubmitAction.class); + private static final String PARAM_PROJECT_KEY = "projectKey"; private static final String PARAM_PROJECT_NAME = "projectName"; private static final String PARAM_REPORT_DATA = "report"; @@ -103,6 +109,9 @@ public class SubmitAction implements CeWsAction { .setProjectId(task.getComponent().get().getUuid()) .build(); WsUtils.writeProtobuf(submitResponse, wsRequest, wsResponse); + } catch (IllegalStateException e) { + LOGGER.debug(e.getMessage(), e); + throw new ServerException(HTTP_INTERNAL_ERROR, e.getMessage()); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/SubmitActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/SubmitActionTest.java index a5048433006..2dba30dadb6 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/SubmitActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/SubmitActionTest.java @@ -27,6 +27,8 @@ import org.mockito.ArgumentCaptor; import org.sonar.ce.task.CeTask; import org.sonar.db.ce.CeTaskTypes; import org.sonar.server.ce.queue.ReportSubmitter; +import org.sonar.server.exceptions.ServerException; +import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; import org.sonar.test.JsonAssert; @@ -35,6 +37,7 @@ import org.sonarqube.ws.MediaTypes; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.entry; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyMap; @@ -171,4 +174,20 @@ public class SubmitActionTest { verify(reportSubmitter).submit(eq("my_project"), eq("my_project"), anyMap(), any()); } + + @Test + public void handle_whenReportSubmitterThrowIllegalStateException_shouldThrowServerException() { + when(reportSubmitter.submit(eq("my_project"), eq("my_project"), anyMap(), any())) + .thenThrow(new IllegalStateException("Error message")); + + TestRequest request = tester.newRequest() + .setParam("projectKey", "my_project") + .setPart("report", new ByteArrayInputStream("{binary}".getBytes()), "foo.bar") + .setMediaType(MediaTypes.PROTOBUF) + .setMethod("POST"); + + assertThatThrownBy(() -> request.execute()) + .isInstanceOf(ServerException.class) + .hasMessage("Error message"); + } } -- 2.39.5