From bdf146ded657052b6bfcd71b75d7e466f907b71c Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Mon, 2 Jul 2012 00:04:49 +0200 Subject: [PATCH] Exists API can send response body, closes #2072. --- src/main/java/org/elasticsearch/ExceptionsHelper.java | 8 ++++++++ .../admin/indices/exists/RestIndicesExistsAction.java | 3 ++- .../elasticsearch/rest/action/get/RestHeadAction.java | 7 +++---- .../rest/action/main/RestMainAction.java | 11 +++++++---- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/elasticsearch/ExceptionsHelper.java b/src/main/java/org/elasticsearch/ExceptionsHelper.java index a4a6cab563a..81b89c6e1a2 100644 --- a/src/main/java/org/elasticsearch/ExceptionsHelper.java +++ b/src/main/java/org/elasticsearch/ExceptionsHelper.java @@ -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; diff --git a/src/main/java/org/elasticsearch/rest/action/admin/indices/exists/RestIndicesExistsAction.java b/src/main/java/org/elasticsearch/rest/action/admin/indices/exists/RestIndicesExistsAction.java index c46643c3421..499481c2dd8 100644 --- a/src/main/java/org/elasticsearch/rest/action/admin/indices/exists/RestIndicesExistsAction.java +++ b/src/main/java/org/elasticsearch/rest/action/admin/indices/exists/RestIndicesExistsAction.java @@ -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); } diff --git a/src/main/java/org/elasticsearch/rest/action/get/RestHeadAction.java b/src/main/java/org/elasticsearch/rest/action/get/RestHeadAction.java index 39450f53270..726dc0b0553 100644 --- a/src/main/java/org/elasticsearch/rest/action/get/RestHeadAction.java +++ b/src/main/java/org/elasticsearch/rest/action/get/RestHeadAction.java @@ -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); } } diff --git a/src/main/java/org/elasticsearch/rest/action/main/RestMainAction.java b/src/main/java/org/elasticsearch/rest/action/main/RestMainAction.java index 0970b6fa263..ecaf67b4593 100644 --- a/src/main/java/org/elasticsearch/rest/action/main/RestMainAction.java +++ b/src/main/java/org/elasticsearch/rest/action/main/RestMainAction.java @@ -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); } }