Skip to content

Commit

Permalink
[Backport] [2.x] Fixed misunderstanding message 'No OpenSearchExcepti…
Browse files Browse the repository at this point in the history
…on found' when detailed_error disabled (opensearch-project#4708) (opensearch-project#5073)

* Fixed misunderstanding message 'No OpenSearchException found' when detailed_error disabled

Signed-off-by: Xue Zhou <[email protected]>
(cherry picked from commit 802e693)

Co-authored-by: Xue Zhou <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] and xuezhou25 authored Nov 4, 2022
1 parent e0d1c2e commit d1b4340
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Fix bug in AwarenessAttributeDecommissionIT([4822](https://github.com/opensearch-project/OpenSearch/pull/4822))
- Fix for failing checkExtraction, checkLicense and checkNotice tasks for windows gradle check ([#4941](https://github.com/opensearch-project/OpenSearch/pull/4941))
- [BUG]: Allow decommission to support delay timeout [#4930](https://github.com/opensearch-project/OpenSearch/pull/4930))
- Fixed misunderstanding message "No OpenSearchException found" when detailed_error disabled by return meaningful messages ([#4708](https://github.com/opensearch-project/OpenSearch/pull/4708))

### Security
- CVE-2022-25857 org.yaml:snakeyaml DOS vulnerability ([#4341](https://github.com/opensearch-project/OpenSearch/pull/4341))

Expand Down
15 changes: 15 additions & 0 deletions server/src/main/java/org/opensearch/ExceptionsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ public static RestStatus status(Throwable t) {
return RestStatus.INTERNAL_SERVER_ERROR;
}

public static String summaryMessage(Throwable t) {
if (t != null) {
if (t instanceof OpenSearchException) {
return t.getClass().getSimpleName() + "[" + t.getMessage() + "]";
} else if (t instanceof IllegalArgumentException) {
return "Invalid argument";
} else if (t instanceof JsonParseException) {
return "Failed to parse JSON";
} else if (t instanceof OpenSearchRejectedExecutionException) {
return "Too many requests";
}
}
return "Internal failure";
}

public static Throwable unwrapCause(Throwable t) {
int counter = 0;
Throwable result = t;
Expand Down
4 changes: 1 addition & 3 deletions server/src/main/java/org/opensearch/OpenSearchException.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,16 +596,14 @@ public static void generateFailureXContent(XContentBuilder builder, Params param

// Render the exception with a simple message
if (detailed == false) {
String message = "No OpenSearchException found";
Throwable t = e;
for (int counter = 0; counter < 10 && t != null; counter++) {
if (t instanceof OpenSearchException) {
message = t.getClass().getSimpleName() + "[" + t.getMessage() + "]";
break;
}
t = t.getCause();
}
builder.field(ERROR, message);
builder.field(ERROR, ExceptionsHelper.summaryMessage(e));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ public void testStatus() {
assertThat(ExceptionsHelper.status(new OpenSearchRejectedExecutionException("rejected")), equalTo(RestStatus.TOO_MANY_REQUESTS));
}

public void testSummaryMessage() {
assertThat(ExceptionsHelper.summaryMessage(new IllegalArgumentException("illegal")), equalTo("Invalid argument"));
assertThat(ExceptionsHelper.summaryMessage(new JsonParseException(null, "illegal")), equalTo("Failed to parse JSON"));
assertThat(ExceptionsHelper.summaryMessage(new OpenSearchRejectedExecutionException("rejected")), equalTo("Too many requests"));
}

public void testGroupBy() {
ShardOperationFailedException[] failures = new ShardOperationFailedException[] {
createShardFailureParsingException("error", "node0", "index", 0, null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,12 +814,7 @@ public void testFailureToAndFromXContentWithNoDetails() throws IOException {
}
assertNotNull(parsedFailure);

String reason;
if (failure instanceof OpenSearchException) {
reason = failure.getClass().getSimpleName() + "[" + failure.getMessage() + "]";
} else {
reason = "No OpenSearchException found";
}
String reason = ExceptionsHelper.summaryMessage(failure);
assertEquals(OpenSearchException.buildMessage("exception", reason, null), parsedFailure.getMessage());
assertEquals(0, parsedFailure.getHeaders().size());
assertEquals(0, parsedFailure.getMetadata().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void testNonOpenSearchExceptionIsNotShownAsSimpleMessage() throws Excepti
assertThat(text, not(containsString("UnknownException[an error occurred reading data]")));
assertThat(text, not(containsString("FileNotFoundException[/foo/bar]")));
assertThat(text, not(containsString("error_trace")));
assertThat(text, containsString("\"error\":\"No OpenSearchException found\""));
assertThat(text, containsString("\"error\":\"Internal failure\""));
}

public void testErrorTrace() throws Exception {
Expand Down

0 comments on commit d1b4340

Please sign in to comment.