From 6ea16bffd566b4790f26b492860f01d9f2cda5e9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 18 May 2016 10:21:36 +0200 Subject: [PATCH] SONAR-7278 show message from response if HTTP 400 --- .../org/sonar/batch/bootstrap/BatchWsClient.java | 9 ++++++--- .../sonar/batch/bootstrap/BatchWsClientTest.java | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/batch/bootstrap/BatchWsClient.java b/sonar-scanner-engine/src/main/java/org/sonar/batch/bootstrap/BatchWsClient.java index d77670a6c86..d40683c8ca8 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/batch/bootstrap/BatchWsClient.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/batch/bootstrap/BatchWsClient.java @@ -25,7 +25,6 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import java.net.HttpURLConnection; import java.util.ArrayList; import java.util.List; import org.sonar.api.CoreProperties; @@ -40,6 +39,9 @@ import org.sonarqube.ws.client.WsRequest; import org.sonarqube.ws.client.WsResponse; import static java.lang.String.format; +import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; +import static java.net.HttpURLConnection.HTTP_FORBIDDEN; +import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED; public class BatchWsClient { @@ -78,7 +80,8 @@ public class BatchWsClient { } private void failIfUnauthorized(WsResponse response) { - if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) { + int code = response.code(); + if (code == HTTP_UNAUTHORIZED) { if (hasCredentials) { // credentials are not valid throw MessageException.of(format("Not authorized. Please check the properties %s and %s.", @@ -89,7 +92,7 @@ public class BatchWsClient { "Please provide the values of the properties %s and %s.", CoreProperties.LOGIN, CoreProperties.PASSWORD)); } - if (response.code() == HttpURLConnection.HTTP_FORBIDDEN) { + if (code == HTTP_FORBIDDEN || code == HTTP_BAD_REQUEST) { // SONAR-4397 Details are in response content throw MessageException.of(tryParseAsJsonError(response.content())); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/batch/bootstrap/BatchWsClientTest.java b/sonar-scanner-engine/src/test/java/org/sonar/batch/bootstrap/BatchWsClientTest.java index 3689c2d601a..41270557b66 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/batch/bootstrap/BatchWsClientTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/batch/bootstrap/BatchWsClientTest.java @@ -106,6 +106,20 @@ public class BatchWsClientTest { new BatchWsClient(wsClient, true).call(request); } + @Test + public void fail_if_bad_request() throws Exception { + expectedException.expect(MessageException.class); + expectedException.expectMessage("Boo! bad request! bad!"); + + WsRequest request = newRequest(); + WsResponse response = newResponse() + .setCode(400) + .setContent("{\"errors\":[{\"msg\":\"Boo! bad request! bad!\"}]}"); + when(wsClient.wsConnector().call(request)).thenReturn(response); + + new BatchWsClient(wsClient, true).call(request); + } + private MockWsResponse newResponse() { return new MockWsResponse().setRequestUrl("https://local/api/issues/search"); } -- 2.39.5