Skip to content

Commit 2da85c9

Browse files
synhershkojasontedor
authored andcommitted
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 elastic#26564
1 parent 7d9d454 commit 2da85c9

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

client/rest/src/main/java/org/elasticsearch/client/ResponseException.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.http.util.EntityUtils;
2525

2626
import java.io.IOException;
27+
import java.util.Locale;
2728

2829
/**
2930
* Exception thrown when an elasticsearch node responds to a request with a status code that indicates an error.
@@ -39,8 +40,13 @@ public ResponseException(Response response) throws IOException {
3940
}
4041

4142
private static String buildMessage(Response response) throws IOException {
42-
String message = response.getRequestLine().getMethod() + " " + response.getHost() + response.getRequestLine().getUri()
43-
+ ": " + response.getStatusLine().toString();
43+
String message = String.format(Locale.ROOT,
44+
"method [%s], host [%s], URI [%s], status line [%s]",
45+
response.getRequestLine().getMethod(),
46+
response.getHost(),
47+
response.getRequestLine().getUri(),
48+
response.getStatusLine().toString()
49+
);
4450

4551
HttpEntity entity = response.getEntity();
4652
if (entity != null) {

client/rest/src/test/java/org/elasticsearch/client/ResponseExceptionTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.io.ByteArrayInputStream;
3737
import java.io.IOException;
3838
import java.nio.charset.StandardCharsets;
39+
import java.util.Locale;
3940

4041
import static org.junit.Assert.assertEquals;
4142
import static org.junit.Assert.assertNull;
@@ -74,8 +75,14 @@ public void testResponseException() throws IOException {
7475
assertNull(responseException.getResponse().getEntity());
7576
}
7677

77-
String message = response.getRequestLine().getMethod() + " " + response.getHost() + response.getRequestLine().getUri()
78-
+ ": " + response.getStatusLine().toString();
78+
String message = String.format(Locale.ROOT,
79+
"method [%s], host [%s], URI [%s], status line [%s]",
80+
response.getRequestLine().getMethod(),
81+
response.getHost(),
82+
response.getRequestLine().getUri(),
83+
response.getStatusLine().toString()
84+
);
85+
7986
if (hasBody) {
8087
message += "\n" + responseBody;
8188
}

0 commit comments

Comments
 (0)