]> source.dussan.org Git - sonarqube.git/commitdiff
Improve error handling in Java WS Client
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 16 Oct 2014 12:12:59 +0000 (14:12 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 16 Oct 2014 12:13:56 +0000 (14:13 +0200)
server/sonar-ws-client/src/main/java/org/sonar/wsclient/base/HttpException.java
server/sonar-ws-client/src/main/java/org/sonar/wsclient/internal/HttpRequestFactory.java
server/sonar-ws-client/src/test/java/org/sonar/wsclient/base/HttpExceptionTest.java

index 756f8f22727e4fa20cf01bff15b3779295fe14ec..840573e308a683bf8fddd63e6119569114fdcc16 100644 (file)
@@ -27,8 +27,8 @@ public class HttpException extends RuntimeException {
   private final String url;
   private final int status;
 
-  public HttpException(String url, int status) {
-    super(String.format("Error %d on %s", status, url));
+  public HttpException(String url, int status, String message) {
+    super(String.format("Error %d on %s : %s", status, url, message));
     this.url = url;
     this.status = status;
   }
index d88cf76c32f66feb3573fd796dac4ae210e4ad4d..bee47cd49e699ee3dbcae059726589e5f3cf34e4 100644 (file)
@@ -27,9 +27,7 @@ import javax.annotation.Nullable;
 import java.util.Arrays;
 import java.util.Map;
 
-import static java.net.HttpURLConnection.HTTP_CREATED;
-import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
-import static java.net.HttpURLConnection.HTTP_OK;
+import static java.net.HttpURLConnection.*;
 
 /**
  * Not an API. Please do not use this class, except maybe for unit tests.
@@ -149,8 +147,8 @@ public class HttpRequestFactory {
       if (isSuccess(request)) {
         return request.body(HttpRequest.CHARSET_UTF8);
       }
-      // TODO handle error messages
-      throw new HttpException(request.url().toString(), request.code());
+      // TODO better handle error messages
+      throw new HttpException(request.url().toString(), request.code(), request.body());
 
     } catch (HttpRequest.HttpRequestException e) {
       throw new IllegalStateException("Fail to request " + request.url(), e.getCause());
index 25f0adc1c2ef0541acc0357ac7702b00eac6ed86..04e3fc048fee7cbace8f4204009dd3b461066646 100644 (file)
@@ -26,9 +26,9 @@ import static org.fest.assertions.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, "Not found");
     assertThat(exception.status()).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 : Not found");
   }
 }