aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2015-01-28 17:14:09 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2015-01-28 18:11:06 +0100
commit27520f113d36d6f419160d7fff79f9ce4cab7b30 (patch)
treea4018c518abbe37c868f29590bcef2c582c5e1eb /sonar-batch
parent0ceb6977631af9a3de12541444bf9b347eb5b0f1 (diff)
downloadsonarqube-27520f113d36d6f419160d7fff79f9ce4cab7b30.tar.gz
sonarqube-27520f113d36d6f419160d7fff79f9ce4cab7b30.zip
Unwrap JSON error message when calling WS
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java24
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java11
2 files changed, 34 insertions, 1 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java
index 2fed7682233..faeef8385d8 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java
@@ -19,10 +19,15 @@
*/
package org.sonar.batch.bootstrap;
+import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
@@ -39,6 +44,8 @@ import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
/**
* Replace the deprecated org.sonar.batch.ServerMetadata
@@ -127,11 +134,26 @@ public class ServerClient implements BatchComponent {
}
if (he.getResponseCode() == 403) {
// SONAR-4397 Details are in response content
- return new IllegalStateException(he.getResponseContent());
+ return new IllegalStateException(tryParseAsJsonError(he.getResponseContent()));
}
return new IllegalStateException(String.format("Fail to execute request [code=%s, url=%s]", he.getResponseCode(), he.getUri()), he);
}
+ private String tryParseAsJsonError(String responseContent) {
+ try {
+ JsonParser parser = new JsonParser();
+ JsonObject obj = parser.parse(responseContent).getAsJsonObject();
+ JsonArray errors = obj.getAsJsonArray("errors");
+ List<String> errorMessages = new ArrayList<>();
+ for (JsonElement e : errors) {
+ errorMessages.add(e.getAsJsonObject().get("msg").getAsString());
+ }
+ return Joiner.on(", ").join(errorMessages);
+ } catch (Exception e) {
+ return responseContent;
+ }
+ }
+
public String getMessageWhenNotAuthorized() {
if (Strings.isNullOrEmpty(getLogin()) && Strings.isNullOrEmpty(getPassword())) {
return "Not authorized. Analyzing this project requires to be authenticated. Please provide the values of the properties %s and %s.";
diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java
index b5843e2bad9..1034c020957 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java
@@ -126,6 +126,17 @@ public class ServerClientTest {
}
@Test
+ public void should_display_json_error_when_403() throws Exception {
+ server = new MockHttpServer();
+ server.start();
+ server.setMockResponseStatus(403);
+ server.setMockResponseData("{\"errors\":[{\"msg\":\"Insufficient privileges\"}]}");
+
+ thrown.expectMessage("Insufficient privileges");
+ newServerClient().request("/foo");
+ }
+
+ @Test
public void should_fail_if_error() throws Exception {
server = new MockHttpServer();
server.start();