From 80f1692be14b4569a910df1fb2a03a51267d7de0 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 8 Sep 2016 10:32:45 +0200 Subject: [PATCH] Return content when exception is generated in ws client --- .../repository/DefaultProjectRepositoriesLoaderTest.java | 7 ++----- .../main/java/org/sonarqube/ws/client/BaseResponse.java | 3 ++- .../main/java/org/sonarqube/ws/client/HttpException.java | 4 ++-- .../test/java/org/sonarqube/ws/client/BaseServiceTest.java | 2 +- .../java/org/sonarqube/ws/client/HttpExceptionTest.java | 4 ++-- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java index b66e883ebe1..3259d288083 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java @@ -31,9 +31,6 @@ import org.junit.rules.ExpectedException; import org.sonar.api.utils.MessageException; import org.sonar.scanner.WsTestUtil; import org.sonar.scanner.bootstrap.BatchWsClient; -import org.sonar.scanner.repository.DefaultProjectRepositoriesLoader; -import org.sonar.scanner.repository.FileData; -import org.sonar.scanner.repository.ProjectRepositories; import org.sonarqube.ws.WsBatch.WsProjectResponse; import org.sonarqube.ws.client.HttpException; import org.sonarqube.ws.client.WsRequest; @@ -76,7 +73,7 @@ public class DefaultProjectRepositoriesLoaderTest { @Test(expected = IllegalStateException.class) public void failFastHttpError() { - HttpException http = new HttpException("url", 403); + HttpException http = new HttpException("url", 403, null); IllegalStateException e = new IllegalStateException("http error", http); WsTestUtil.mockException(wsClient, e); loader.load(PROJECT_KEY, false); @@ -87,7 +84,7 @@ public class DefaultProjectRepositoriesLoaderTest { thrown.expect(MessageException.class); thrown.expectMessage("http error"); - HttpException http = new HttpException("uri", 403); + HttpException http = new HttpException("uri", 403, null); MessageException e = MessageException.of("http error", http); WsTestUtil.mockException(wsClient, e); loader.load(PROJECT_KEY, false); diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseResponse.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseResponse.java index f97a919ec91..6949f4fe9a6 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseResponse.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/BaseResponse.java @@ -31,8 +31,9 @@ abstract class BaseResponse implements WsResponse { @Override public WsResponse failIfNotSuccessful() { if (!isSuccessful()) { + String content = content(); close(); - throw new HttpException(requestUrl(), code()); + throw new HttpException(requestUrl(), code(), content); } return this; } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpException.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpException.java index b69cd482679..462145d6a45 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpException.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/HttpException.java @@ -27,8 +27,8 @@ public class HttpException extends RuntimeException { private final String url; private final int code; - public HttpException(String url, int code) { - super(String.format("Error %d on %s", code, url)); + 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; } diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseServiceTest.java index e7e0a43dc2d..5b1b8d411ae 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseServiceTest.java @@ -74,7 +74,7 @@ public class BaseServiceTest { public void test() { GetRequest get = new GetRequest(path("issue")).setParam("key", "ABC"); - when(wsConnector.call(get)).thenReturn(new MockWsResponse().setCode(403).setRequestUrl("https://local/foo")); + when(wsConnector.call(get)).thenReturn(new MockWsResponse().setCode(403).setRequestUrl("https://local/foo").setContent("error")); try { call(get, Testing.Fake.parser()); diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpExceptionTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpExceptionTest.java index 17ed590f744..c379a4f8851 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpExceptionTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpExceptionTest.java @@ -26,9 +26,9 @@ import static org.assertj.core.api.Assertions.assertThat; public class HttpExceptionTest { @Test public void test_exception() throws Exception { - HttpException exception = new HttpException("http://localhost:9000/api/search", 500); + HttpException exception = new HttpException("http://localhost:9000/api/search", 500, "error"); assertThat(exception.code()).isEqualTo(500); assertThat(exception.url()).isEqualTo("http://localhost:9000/api/search"); - assertThat(exception.getMessage()).isEqualTo("Error 500 on http://localhost:9000/api/search"); + assertThat(exception.getMessage()).isEqualTo("Error 500 on http://localhost:9000/api/search : error"); } } -- 2.39.5