diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-05-18 10:21:36 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-05-18 17:42:20 +0200 |
commit | 6ea16bffd566b4790f26b492860f01d9f2cda5e9 (patch) | |
tree | f56c8e0e34c3457191cd997deb8f79a5aec6fa07 /sonar-scanner-engine | |
parent | a0bbadad133d1ab014cbce1b775c54ac367d7862 (diff) | |
download | sonarqube-6ea16bffd566b4790f26b492860f01d9f2cda5e9.tar.gz sonarqube-6ea16bffd566b4790f26b492860f01d9f2cda5e9.zip |
SONAR-7278 show message from response if HTTP 400
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r-- | sonar-scanner-engine/src/main/java/org/sonar/batch/bootstrap/BatchWsClient.java | 9 | ||||
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/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"); } |