Skip to content

Commit

Permalink
[REST] Render non-elasticsearch exception as root cause
Browse files Browse the repository at this point in the history
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 elastic#10836
  • Loading branch information
s1monw committed Apr 28, 2015
1 parent 54f2a91 commit 0e9d703
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
13 changes: 11 additions & 2 deletions src/main/java/org/elasticsearch/ElasticsearchException.java
Expand Up @@ -194,7 +194,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
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);
}
Expand Down Expand Up @@ -261,7 +261,16 @@ public static ElasticsearchException[] guessRootCauses(Throwable t) {
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);
}

/**
Expand Down
Expand Up @@ -104,6 +104,15 @@ public void testGuessRootCause() {

}

{
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 {
Expand Down
17 changes: 12 additions & 5 deletions src/test/java/org/elasticsearch/rest/BytesRestResponseTests.java
Expand Up @@ -112,11 +112,18 @@ public void testErrorTrace() throws Exception {
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
Expand Down

0 comments on commit 0e9d703

Please sign in to comment.