]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20699 Propagate meaningful exceptions to the Scanner
authorAntoine Vigneau <antoine.vigneau@sonarsource.com>
Thu, 19 Oct 2023 14:07:56 +0000 (16:07 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 20 Oct 2023 20:02:40 +0000 (20:02 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/SubmitAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/SubmitActionTest.java

index 3f9fec277e6fed81a1a7fc4655c2a42744deac4c..1e6c9e2acc95600f63051b6e7c05bccead4eb521 100644 (file)
@@ -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());
     }
   }
 
index a5048433006563724598ffdd05bbc6df690ac647..2dba30dadb6afa2b2c6255fa432eaa3f5161788d 100644 (file)
@@ -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");
+  }
 }