From ac32f3d31083040f9e604cbad127ff8fbc7e0763 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Fri, 3 Jul 2015 12:13:56 +0200 Subject: [PATCH] Don't special-case on ElasticsearchWrapperException in toXContent the specialization can cause stack overflows if an exception is a ElasticsearchWrapperException as well as a ElasticsearchException. This commit just relies on the unwrap logic now to find the cause and only renders if we the rendering exception is the cause otherwise forwards to the generic exception rendering. Closes #11994 --- .../org/elasticsearch/ElasticsearchException.java | 3 ++- .../elasticsearch/ElasticsearchExceptionTests.java | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/ElasticsearchException.java b/core/src/main/java/org/elasticsearch/ElasticsearchException.java index 1fa95d6b1b0..789589f3f64 100644 --- a/core/src/main/java/org/elasticsearch/ElasticsearchException.java +++ b/core/src/main/java/org/elasticsearch/ElasticsearchException.java @@ -246,7 +246,8 @@ public class ElasticsearchException extends RuntimeException implements ToXConte @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - if (this instanceof ElasticsearchWrapperException) { + Throwable ex = ExceptionsHelper.unwrapCause(this); + if (ex != this) { toXContent(builder, params, this); } else { builder.field("type", getExceptionName()); diff --git a/core/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java b/core/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java index 34bc1178b02..581868ff395 100644 --- a/core/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java +++ b/core/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java @@ -32,14 +32,17 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentLocation; import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexException; import org.elasticsearch.index.query.QueryParsingException; import org.elasticsearch.index.query.TestQueryParsingException; import org.elasticsearch.indices.IndexMissingException; import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.search.SearchParseException; import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.test.ElasticsearchTestCase; +import org.elasticsearch.test.TestSearchContext; import org.elasticsearch.test.VersionUtils; import org.elasticsearch.test.hamcrest.ElasticsearchAssertions; import org.elasticsearch.transport.RemoteTransportException; @@ -177,6 +180,16 @@ public class ElasticsearchExceptionTests extends ElasticsearchTestCase { } public void testToXContent() throws IOException { + { + ElasticsearchException ex = new SearchParseException(new TestSearchContext(), "foo", new XContentLocation(1,0)); + XContentBuilder builder = XContentFactory.jsonBuilder(); + builder.startObject(); + ex.toXContent(builder, ToXContent.EMPTY_PARAMS); + builder.endObject(); + + String expected = "{\"type\":\"search_parse_exception\",\"reason\":\"foo\",\"line\":1,\"col\":0}"; + assertEquals(expected, builder.string()); + } { ElasticsearchException ex = new ElasticsearchException("foo", new ElasticsearchException("bar", new IllegalArgumentException("index is closed", new RuntimeException("foobar")))); XContentBuilder builder = XContentFactory.jsonBuilder(); @@ -226,6 +239,7 @@ public class ElasticsearchExceptionTests extends ElasticsearchTestCase { ex.toXContent(otherBuilder, ToXContent.EMPTY_PARAMS); otherBuilder.endObject(); assertEquals(otherBuilder.string(), builder.string()); + assertEquals("{\"type\":\"file_not_found_exception\",\"reason\":\"foo not found\"}", builder.string()); } }