]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15208 Scanner should distinguish between network and server errors when uploadi...
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Tue, 14 Sep 2021 18:35:18 +0000 (13:35 -0500)
committersonartech <sonartech@sonarsource.com>
Thu, 16 Sep 2021 20:03:31 +0000 (20:03 +0000)
sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java

index f5a6e50b33e58c794b19336b117d2658e37a1220..f628cdc351c8a2a77cdfa14f0941f87f8fa0bc6f 100644 (file)
@@ -202,11 +202,16 @@ public class ReportPublisher implements Startable {
     WsResponse response;
     try {
       post.setWriteTimeOutInMs(DEFAULT_WRITE_TIMEOUT);
-      response = wsClient.call(post).failIfNotSuccessful();
-    } catch (HttpException e) {
-      throw MessageException.of(String.format("Failed to upload report - %s", DefaultScannerWsClient.createErrorMessage(e)));
+      response = wsClient.call(post);
+    } catch (Exception e) {
+      throw new IllegalStateException("Failed to upload report: " + e.getMessage(), e);
     }
 
+    try {
+      response.failIfNotSuccessful();
+    } catch (HttpException e) {
+      throw MessageException.of(String.format("Server failed to process report. Please check server logs: %s", DefaultScannerWsClient.createErrorMessage(e)));
+    }
     try (InputStream protobuf = response.contentStream()) {
       return Ce.SubmitResponse.parser().parseFrom(protobuf).getTaskId();
     } catch (Exception e) {
index f28488eb69a1c31a871f2c4afd7c0e649bfb027a..5833168be7e9ab8c37b0ecd81e936a48041ab16e 100644 (file)
@@ -71,16 +71,16 @@ public class ReportPublisherTest {
   @Rule
   public ExpectedException exception = ExpectedException.none();
 
-  GlobalAnalysisMode mode = mock(GlobalAnalysisMode.class);
-  ScanProperties properties = mock(ScanProperties.class);
-  DefaultScannerWsClient wsClient = mock(DefaultScannerWsClient.class, Mockito.RETURNS_DEEP_STUBS);
-  Server server = mock(Server.class);
-  InputModuleHierarchy moduleHierarchy = mock(InputModuleHierarchy.class);
-  DefaultInputModule root;
-  AnalysisContextReportPublisher contextPublisher = mock(AnalysisContextReportPublisher.class);
-  BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
-  CeTaskReportDataHolder reportMetadataHolder = mock(CeTaskReportDataHolder.class);
-  ReportPublisher underTest = new ReportPublisher(properties, wsClient, server, contextPublisher, moduleHierarchy, mode, reportTempFolder,
+  private GlobalAnalysisMode mode = mock(GlobalAnalysisMode.class);
+  private ScanProperties properties = mock(ScanProperties.class);
+  private DefaultScannerWsClient wsClient = mock(DefaultScannerWsClient.class, Mockito.RETURNS_DEEP_STUBS);
+  private Server server = mock(Server.class);
+  private InputModuleHierarchy moduleHierarchy = mock(InputModuleHierarchy.class);
+  private DefaultInputModule root;
+  private AnalysisContextReportPublisher contextPublisher = mock(AnalysisContextReportPublisher.class);
+  private BranchConfiguration branchConfiguration = mock(BranchConfiguration.class);
+  private CeTaskReportDataHolder reportMetadataHolder = mock(CeTaskReportDataHolder.class);
+  private ReportPublisher underTest = new ReportPublisher(properties, wsClient, server, contextPublisher, moduleHierarchy, mode, reportTempFolder,
     new ReportPublisherStep[0], branchConfiguration, reportMetadataHolder);
 
   @Before
@@ -120,6 +120,18 @@ public class ReportPublisherTest {
         "ceTaskUrl=https://localhost/api/ce/task?id=TASK-123\n");
   }
 
+  @Test
+  public void upload_error_message() {
+    HttpException ex = new HttpException("url", 404, "{\"errors\":[{\"msg\":\"Organization with key 'MyOrg' does not exist\"}]}");
+    WsResponse response = mock(WsResponse.class);
+    when(response.failIfNotSuccessful()).thenThrow(ex);
+    when(wsClient.call(any(WsRequest.class))).thenThrow(new IllegalStateException("timeout"));
+
+    exception.expect(IllegalStateException.class);
+    exception.expectMessage("Failed to upload report: timeout");
+    underTest.upload(reportTempFolder.newFile());
+  }
+
   @Test
   public void parse_upload_error_message() {
     HttpException ex = new HttpException("url", 404, "{\"errors\":[{\"msg\":\"Organization with key 'MyOrg' does not exist\"}]}");
@@ -128,7 +140,7 @@ public class ReportPublisherTest {
     when(wsClient.call(any(WsRequest.class))).thenReturn(response);
 
     exception.expect(MessageException.class);
-    exception.expectMessage("Failed to upload report - Organization with key 'MyOrg' does not exist");
+    exception.expectMessage("Server failed to process report. Please check server logs: Organization with key 'MyOrg' does not exist");
     underTest.upload(reportTempFolder.newFile());
   }