[REST] Render non-elasticsearch exception as root cause

if we don't have an ElasticsearchException as the wrapper of the
actual cause we don't render a root cause today. This commit adds
support for 3rd party exceptions as root causes.

Closes #10836
This commit is contained in:
Simon Willnauer 2015-04-28 13:59:20 +02:00
parent 04f6067c66
commit 87cf1452d5
3 changed files with 32 additions and 7 deletions

View File

@ -194,7 +194,7 @@ public class ElasticsearchException extends RuntimeException implements ToXConte
if (this instanceof ElasticsearchWrapperException) {
toXContent(builder, params, this);
} else {
builder.field("type", getExceptionName(this));
builder.field("type", getExceptionName());
builder.field("reason", getMessage());
innerToXContent(builder, params);
}
@ -261,7 +261,16 @@ public class ElasticsearchException extends RuntimeException implements ToXConte
if (ex instanceof ElasticsearchException) {
return ((ElasticsearchException) ex).guessRootCauses();
}
return new ElasticsearchException[0];
return new ElasticsearchException[] {new ElasticsearchException(t.getMessage(), t) {
@Override
protected String getExceptionName() {
return getExceptionName(getCause());
}
}};
}
protected String getExceptionName() {
return getExceptionName(this);
}
/**

View File

@ -104,6 +104,15 @@ public class ElasticsearchExceptionTests extends ElasticsearchTestCase {
}
{
final ElasticsearchException[] foobars = ElasticsearchException.guessRootCauses(new IllegalArgumentException("foobar"));
assertEquals(foobars.length, 1);
assertTrue(foobars[0] instanceof ElasticsearchException);
assertEquals(foobars[0].getMessage(), "foobar");
assertEquals(foobars[0].getCause().getClass(), IllegalArgumentException.class);
assertEquals(foobars[0].getExceptionName(), "illegal_argument_exception");
}
}
public void testDeduplicate() throws IOException {

View File

@ -112,11 +112,18 @@ public class BytesRestResponseTests extends ElasticsearchTestCase {
public void testGuessRootCause() throws IOException {
RestRequest request = new FakeRestRequest();
RestChannel channel = new DetailedExceptionRestChannel(request);
Throwable t = new ElasticsearchException("an error occurred reading data", new FileNotFoundException("/foo/bar"));
BytesRestResponse response = new BytesRestResponse(channel, t);
String text = response.content().toUtf8();
assertThat(text, containsString("{\"root_cause\":[{\"type\":\"exception\",\"reason\":\"an error occurred reading data\"}]"));
{
Throwable t = new ElasticsearchException("an error occurred reading data", new FileNotFoundException("/foo/bar"));
BytesRestResponse response = new BytesRestResponse(channel, t);
String text = response.content().toUtf8();
assertThat(text, containsString("{\"root_cause\":[{\"type\":\"exception\",\"reason\":\"an error occurred reading data\"}]"));
}
{
Throwable t = new FileNotFoundException("/foo/bar");
BytesRestResponse response = new BytesRestResponse(channel, t);
String text = response.content().toUtf8();
assertThat(text, containsString("{\"root_cause\":[{\"type\":\"file_not_found_exception\",\"reason\":\"/foo/bar\"}]"));
}
}
@Test