return the response as part of ElasticsearchResponseException

This commit is contained in:
javanna 2016-05-06 15:04:53 +02:00 committed by Luca Cavanna
parent e85ed3eb52
commit e77ab87926
4 changed files with 16 additions and 19 deletions

View File

@ -21,7 +21,7 @@ package org.elasticsearch.client;
import org.apache.http.HttpHost;
import org.apache.http.RequestLine;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import java.io.IOException;
@ -32,25 +32,26 @@ public class ElasticsearchResponseException extends IOException {
private final HttpHost host;
private final RequestLine requestLine;
private final StatusLine statusLine;
private final CloseableHttpResponse response;
public ElasticsearchResponseException(RequestLine requestLine, HttpHost host, StatusLine statusLine) {
super(buildMessage(requestLine, host, statusLine));
public ElasticsearchResponseException(RequestLine requestLine, HttpHost host, CloseableHttpResponse response) {
super(buildMessage(requestLine, host, response));
this.host = host;
this.requestLine = requestLine;
this.statusLine = statusLine;
this.response = response;
}
private static String buildMessage(RequestLine requestLine, HttpHost host, StatusLine statusLine) {
return requestLine.getMethod() + " " + host + requestLine.getUri() + ": " + statusLine.toString();
private static String buildMessage(RequestLine requestLine, HttpHost host, CloseableHttpResponse response) {
return requestLine.getMethod() + " " + host + requestLine.getUri() + ": " + response.getStatusLine().toString();
}
/**
* Returns whether the error is recoverable or not, hence whether the same request should be retried on other nodes or not
*/
public boolean isRecoverable() {
int statusCode = response.getStatusLine().getStatusCode();
//clients don't retry on 500 because elasticsearch still misuses it instead of 400 in some places
return statusLine.getStatusCode() >= 502 && statusLine.getStatusCode() <= 504;
return statusCode >= 502 && statusCode <= 504;
}
/**
@ -68,9 +69,9 @@ public class ElasticsearchResponseException extends IOException {
}
/**
* Returns the {@link StatusLine} that was returned by elasticsearch
* Returns the {@link CloseableHttpResponse} that was returned by elasticsearch
*/
public StatusLine getStatusLine() {
return statusLine;
public CloseableHttpResponse getResponse() {
return response;
}
}

View File

@ -30,7 +30,6 @@ import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.Closeable;
import java.io.IOException;
@ -134,9 +133,8 @@ public final class RestClient implements Closeable {
RequestLogger.log(logger, "request succeeded", request.getRequestLine(), connection.getHost(), response.getStatusLine());
return new ElasticsearchResponse(request.getRequestLine(), connection.getHost(), response);
} else {
EntityUtils.consume(response.getEntity());
RequestLogger.log(logger, "request failed", request.getRequestLine(), connection.getHost(), response.getStatusLine());
throw new ElasticsearchResponseException(request.getRequestLine(), connection.getHost(), statusLine);
throw new ElasticsearchResponseException(request.getRequestLine(), connection.getHost(), response);
}
}

View File

@ -31,7 +31,6 @@ import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.ElasticsearchResponseException;
import org.elasticsearch.client.RequestLogger;
@ -78,8 +77,7 @@ final class Sniffer {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() >= 300) {
RequestLogger.log(logger, "sniff failed", httpGet.getRequestLine(), host, statusLine);
EntityUtils.consume(response.getEntity());
throw new ElasticsearchResponseException(httpGet.getRequestLine(), host, statusLine);
throw new ElasticsearchResponseException(httpGet.getRequestLine(), host, response);
} else {
List<HttpHost> nodes = readHosts(response.getEntity());
RequestLogger.log(logger, "sniff succeeded", httpGet.getRequestLine(), host, statusLine);

View File

@ -107,10 +107,10 @@ public class SnifferTests extends LuceneTestCase {
"/_nodes/http?timeout=" + sniffRequestTimeout));
assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode)));
assertThat(e.getHost(), equalTo(httpHost));
assertThat(e.getStatusLine().getStatusCode(), equalTo(sniffResponse.nodesInfoResponseCode));
assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(sniffResponse.nodesInfoResponseCode));
assertThat(e.getRequestLine().toString(), equalTo("GET /_nodes/http?timeout=" + sniffRequestTimeout + "ms HTTP/1.1"));
} else {
fail("sniffNodes should have succeeded: " + e.getStatusLine());
fail("sniffNodes should have succeeded: " + e.getResponse().getStatusLine());
}
}
}