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
This commit is contained in:
Itamar Syn-Hershko 2017-09-15 04:10:12 +03:00 committed by Jason Tedor
parent 120ddd99c3
commit e9deb62546
2 changed files with 17 additions and 4 deletions

View File

@ -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) {

View File

@ -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;
}