From 7eae121d7dead1f4d8e4016dc6007ada53c28c02 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Mon, 23 Jan 2023 09:00:02 -0500 Subject: [PATCH] Fix: do not double-wrap OpenSearchException. (#323) (#332) Signed-off-by: dblock (cherry picked from commit 37d48000b0aa0dea82b5404a82a05410e388859e) Co-authored-by: Daniel (dB.) Doubrovkine --- CHANGELOG.md | 1 + .../client/transport/aws/AwsSdk2Transport.java | 2 ++ .../integTest/aws/AwsSdk2SearchIT.java | 17 +++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f73dd9e9b8..1d0e1f224f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java b/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java index af55aa02f7..c567230559 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java +++ b/java-client/src/main/java/org/opensearch/client/transport/aws/AwsSdk2Transport.java @@ -526,6 +526,8 @@ private 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(); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SearchIT.java b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SearchIT.java index b138ff8545..4b7a4578ad 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SearchIT.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2SearchIT.java @@ -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; @@ -133,4 +136,18 @@ private CompletableFuture 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]")); + } }