Exists API can send response body, closes #2072.

This commit is contained in:
Shay Banon 2012-07-02 00:04:49 +02:00
parent 3ab85bacdd
commit bdf146ded6
4 changed files with 20 additions and 9 deletions

View File

@ -21,6 +21,7 @@ package org.elasticsearch;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.rest.RestStatus;
/**
*
@ -29,6 +30,13 @@ public final class ExceptionsHelper {
private static final ESLogger logger = Loggers.getLogger(ExceptionsHelper.class);
public static RestStatus status(Throwable t) {
if (t instanceof ElasticSearchException) {
return ((ElasticSearchException) t).status();
}
return RestStatus.INTERNAL_SERVER_ERROR;
}
public static Throwable unwrapCause(Throwable t) {
int counter = 0;
Throwable result = t;

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest.action.admin.indices.exists;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.exists.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.IndicesExistsResponse;
@ -70,7 +71,7 @@ public class RestIndicesExistsAction extends BaseRestHandler {
@Override
public void onFailure(Throwable e) {
try {
channel.sendResponse(new XContentThrowableRestResponse(request, e));
channel.sendResponse(new StringRestResponse(ExceptionsHelper.status(e)));
} catch (Exception e1) {
logger.error("Failed to send failure response", e1);
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest.action.get;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
@ -28,8 +29,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.*;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.HEAD;
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
import static org.elasticsearch.rest.RestStatus.OK;
@ -76,8 +75,8 @@ public class RestHeadAction extends BaseRestHandler {
@Override
public void onFailure(Throwable e) {
try {
channel.sendResponse(new XContentThrowableRestResponse(request, e));
} catch (IOException e1) {
channel.sendResponse(new StringRestResponse(ExceptionsHelper.status(e)));
} catch (Exception e1) {
logger.error("Failed to send failure response", e1);
}
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest.action.main;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
@ -31,8 +32,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestRequest.Method.HEAD;
@ -88,8 +87,12 @@ public class RestMainAction extends BaseRestHandler {
@Override
public void onFailure(Throwable e) {
try {
channel.sendResponse(new XContentThrowableRestResponse(request, e));
} catch (IOException e1) {
if (request.method() == HEAD) {
channel.sendResponse(new StringRestResponse(ExceptionsHelper.status(e)));
} else {
channel.sendResponse(new XContentThrowableRestResponse(request, e));
}
} catch (Exception e1) {
logger.warn("Failed to send response", e);
}
}