]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8783 Improve display of error when uploading scanner report
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Wed, 1 Mar 2017 12:47:39 +0000 (13:47 +0100)
committerdbmeneses <duarte.meneses@sonarsource.com>
Wed, 1 Mar 2017 16:21:11 +0000 (17:21 +0100)
sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/HttpException.java

index f1c87bdf71d3245c0698f0a82c07b544c0ad86ad..19e0169e182efe581794ffb70dd4eef709f68d89 100644 (file)
@@ -106,7 +106,7 @@ public class ScannerWsClient {
     response.failIfNotSuccessful();
   }
 
-  private static String tryParseAsJsonError(String responseContent) {
+  public static String tryParseAsJsonError(String responseContent) {
     try {
       JsonParser parser = new JsonParser();
       JsonObject obj = parser.parse(responseContent).getAsJsonObject();
index 0f4e2ecf1e67d13dd6ffdbba17bf7300431ead8d..c06f344c8ab2289bbbbd21bfbe5fcaf5eef8e6c4 100644 (file)
@@ -52,6 +52,7 @@ import org.sonar.scanner.protocol.output.ScannerReportWriter;
 import org.sonar.scanner.scan.ImmutableProjectReactor;
 import org.sonarqube.ws.MediaTypes;
 import org.sonarqube.ws.WsCe;
+import org.sonarqube.ws.client.HttpException;
 import org.sonarqube.ws.client.PostRequest;
 import org.sonarqube.ws.client.WsResponse;
 
@@ -174,7 +175,14 @@ public class ReportPublisher implements Startable {
       .setParam("projectName", projectDefinition.getOriginalName())
       .setParam("projectBranch", projectDefinition.getBranch())
       .setPart("report", filePart);
-    WsResponse response = wsClient.call(post).failIfNotSuccessful();
+
+    WsResponse response;
+    try {
+      response = wsClient.call(post).failIfNotSuccessful();
+    } catch (HttpException e) {
+      throw MessageException.of(String.format("Failed to upload report - %d: %s", e.code(), ScannerWsClient.tryParseAsJsonError(e.content())));
+    }
+
     try (InputStream protobuf = response.contentStream()) {
       return WsCe.SubmitResponse.parser().parseFrom(protobuf).getTaskId();
     } catch (Exception e) {
index 118b460b740d5c67ad9f7d1285d3607bb112e8d4..a47550ba72156a4ecd1013816be2e8b69a34d0a6 100644 (file)
@@ -47,6 +47,7 @@ import org.sonar.scanner.analysis.DefaultAnalysisMode;
 import org.sonar.scanner.bootstrap.ScannerWsClient;
 import org.sonar.scanner.scan.ImmutableProjectReactor;
 import org.sonarqube.ws.WsCe;
+import org.sonarqube.ws.client.HttpException;
 import org.sonarqube.ws.client.WsRequest;
 import org.sonarqube.ws.client.WsResponse;
 
@@ -71,7 +72,7 @@ public class ReportPublisherTest {
 
   DefaultAnalysisMode mode = mock(DefaultAnalysisMode.class);
   Settings settings = new MapSettings(new PropertyDefinitions(CorePropertyDefinitions.all()));
-  ScannerWsClient wsClient = mock(ScannerWsClient.class, Mockito.RETURNS_DEEP_STUBS);
+  ScannerWsClient wsClient;
   Server server = mock(Server.class);
   ImmutableProjectReactor reactor = mock(ImmutableProjectReactor.class);
   ProjectDefinition root;
@@ -79,6 +80,7 @@ public class ReportPublisherTest {
 
   @Before
   public void setUp() {
+    wsClient = mock(ScannerWsClient.class, Mockito.RETURNS_DEEP_STUBS);
     root = ProjectDefinition.create().setKey("struts").setWorkDir(temp.getRoot());
     when(reactor.getRoot()).thenReturn(root);
     when(server.getPublicRootUrl()).thenReturn("https://localhost");
@@ -108,6 +110,19 @@ public class ReportPublisherTest {
         "ceTaskUrl=https://localhost/api/ce/task?id=TASK-123\n");
   }
 
+  @Test
+  public void parse_upload_error_message() throws IOException {
+    ReportPublisher underTest = new ReportPublisher(settings, wsClient, server, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);
+    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))).thenReturn(response);
+
+    exception.expect(MessageException.class);
+    exception.expectMessage("Failed to upload report - 404: Organization with key 'MyOrg' does not exist");
+    underTest.upload(temp.newFile());
+  }
+
   @Test
   public void log_public_url_if_defined() throws IOException {
     when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube");
index c1ae118726c59080cb9465cefc7b5908c49ea785..d456faeb473411d9d0693ae1a19d8687b4299ced 100644 (file)
@@ -26,11 +26,17 @@ public class HttpException extends RuntimeException {
 
   private final String url;
   private final int code;
+  private final String content;
 
   public HttpException(String url, int code, String content) {
     super(String.format("Error %d on %s : %s", code, url, content));
     this.url = url;
     this.code = code;
+    this.content = content;
+  }
+  
+  public String content() {
+    return content;
   }
 
   public String url() {