Skip to content

Commit

Permalink
Fix: do not double-wrap OpenSearchException. (opensearch-project#323) (
Browse files Browse the repository at this point in the history
…opensearch-project#332)

Signed-off-by: dblock <dblock@amazon.com>

(cherry picked from commit 37d4800)

Co-authored-by: Daniel (dB.) Doubrovkine <dblock@amazon.com>
  • Loading branch information
opensearch-trigger-bot[bot] and dblock authored Jan 23, 2023
1 parent 80607fa commit 7eae121
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Fixed
- Make ChildrenAggregate as a SingleBucketAggregate ([#306](https://github.com/opensearch-project/opensearch-java/pull/306))
- Fix /_nodes/stats, /_nodes/info throwing serialization error ([#315](https://github.com/opensearch-project/opensearch-java/pull/315))
- Do not double-wrap OpenSearchException on error ([#323](https://github.com/opensearch-project/opensearch-java/pull/323))
- Fix AwsSdk2TransportOptions.responseCompression ([#322](https://github.com/opensearch-project/opensearch-java/pull/322))

### Security
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ private <ResponseT, ErrorT> ResponseT parseResponse(
ErrorT error = errorDeserializer.deserialize(parser, mapper);
throw new OpenSearchException((ErrorResponse) error);
}
} catch (OpenSearchException e) {
throw e;
} catch (Exception e) {
// can't parse the error - use a general exception
ErrorCause.Builder cause = new ErrorCause.Builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
import org.opensearch.client.opensearch.OpenSearchAsyncClient;
import org.opensearch.client.opensearch.OpenSearchClient;
import org.opensearch.client.opensearch._types.OpType;
import org.opensearch.client.opensearch._types.OpenSearchException;
import org.opensearch.client.opensearch._types.Refresh;
import org.opensearch.client.opensearch.core.IndexRequest;
import org.opensearch.client.opensearch.core.IndexResponse;
import org.opensearch.client.opensearch.core.SearchResponse;
import org.opensearch.client.opensearch.indices.CreateIndexRequest;
import org.opensearch.client.opensearch.indices.OpenSearchIndicesClient;

import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -133,4 +136,18 @@ private CompletableFuture<IndexResponse> addDoc(
return CompletableFuture.failedFuture(e);
}
}

@Test
public void testDoubleWrappedException() throws Exception {
// ensure the test index exists
resetTestIndex(false);
// attempt to create the same index a second time
OpenSearchIndicesClient client = getIndexesClient(false, null, null);
var req = new CreateIndexRequest.Builder().index(TEST_INDEX);
Exception exception = Assert.assertThrows(OpenSearchException.class, () -> {
client.create(req.build());
});
// error message contains the actual error, not a generic [http_exception]
Assert.assertTrue(exception.getMessage().contains("[resource_already_exists_exception]"));
}
}

0 comments on commit 7eae121

Please sign in to comment.