Browse Source

Better message text for ResponseException

This avoids messages with malformed URLs, like
"org.elasticsearch.client.ResponseException: PUT
http://127.0.0.1:9502customer: HTTP/1.1 400 Bad Request".

Relates #26564
Itamar Syn-Hershko 8 years ago
parent
commit
e9deb62546

+ 8 - 2
client/rest/src/main/java/org/elasticsearch/client/ResponseException.java

@@ -24,6 +24,7 @@ import org.apache.http.entity.BufferedHttpEntity;
 import org.apache.http.util.EntityUtils;
 
 import java.io.IOException;
+import java.util.Locale;
 
 /**
  * Exception thrown when an elasticsearch node responds to a request with a status code that indicates an error.
@@ -39,8 +40,13 @@ public final class ResponseException extends IOException {
     }
 
     private static String buildMessage(Response response) throws IOException {
-        String message = response.getRequestLine().getMethod() + " " + response.getHost() + response.getRequestLine().getUri()
-                + ": " + response.getStatusLine().toString();
+        String message = String.format(Locale.ROOT,
+            "method [%s], host [%s], URI [%s], status line [%s]",
+            response.getRequestLine().getMethod(),
+            response.getHost(),
+            response.getRequestLine().getUri(),
+            response.getStatusLine().toString()
+        );
 
         HttpEntity entity = response.getEntity();
         if (entity != null) {

+ 9 - 2
client/rest/src/test/java/org/elasticsearch/client/ResponseExceptionTests.java

@@ -36,6 +36,7 @@ import org.apache.http.util.EntityUtils;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
+import java.util.Locale;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
@@ -74,8 +75,14 @@ public class ResponseExceptionTests extends RestClientTestCase {
             assertNull(responseException.getResponse().getEntity());
         }
 
-        String message = response.getRequestLine().getMethod() + " " + response.getHost() + response.getRequestLine().getUri()
-                + ": " + response.getStatusLine().toString();
+        String message = String.format(Locale.ROOT,
+            "method [%s], host [%s], URI [%s], status line [%s]",
+            response.getRequestLine().getMethod(),
+            response.getHost(),
+            response.getRequestLine().getUri(),
+            response.getStatusLine().toString()
+        );
+
         if (hasBody) {
             message += "\n" + responseBody;
         }