aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2017-03-01 13:47:39 +0100
committerdbmeneses <duarte.meneses@sonarsource.com>2017-03-01 17:21:11 +0100
commit9c74dc7710f74e5b60beefdb6949a022a5ed2c6a (patch)
tree07d8a047363543c19d60d91c82705107e95bd5cd /sonar-scanner-engine/src
parentc723b844bed1ef2afdc3f5e5779f99fe598b6ac0 (diff)
downloadsonarqube-9c74dc7710f74e5b60beefdb6949a022a5ed2c6a.tar.gz
sonarqube-9c74dc7710f74e5b60beefdb6949a022a5ed2c6a.zip
SONAR-8783 Improve display of error when uploading scanner report
Diffstat (limited to 'sonar-scanner-engine/src')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java2
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java10
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java17
3 files changed, 26 insertions, 3 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java
index f1c87bdf71d..19e0169e182 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClient.java
@@ -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();
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java
index 0f4e2ecf1e6..c06f344c8ab 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ReportPublisher.java
@@ -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) {
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java
index 118b460b740..a47550ba721 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java
@@ -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");
@@ -109,6 +111,19 @@ public class ReportPublisherTest {
}
@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");
ReportPublisher underTest = new ReportPublisher(settings, wsClient, server, contextPublisher, reactor, mode, mock(TempFolder.class), new ReportPublisherStep[0]);