diff --git a/common/src/main/java/org/opensearch/sdk/SdkClient.java b/common/src/main/java/org/opensearch/sdk/SdkClient.java index f9d31b3e00..78f3d8b9a5 100644 --- a/common/src/main/java/org/opensearch/sdk/SdkClient.java +++ b/common/src/main/java/org/opensearch/sdk/SdkClient.java @@ -42,16 +42,7 @@ default PutDataObjectResponse putDataObject(PutDataObjectRequest request) { try { return putDataObjectAsync(request).toCompletableFuture().join(); } catch (CompletionException e) { - Throwable cause = e.getCause(); - if (cause instanceof InterruptedException) { - Thread.currentThread().interrupt(); - } - // Rethrow unchecked Exceptions - if (cause instanceof RuntimeException) { - throw (RuntimeException) cause; - } else { - throw new OpenSearchException(cause); - } + throw unwrapAndConvertToRuntime(e); } } @@ -81,16 +72,7 @@ default GetDataObjectResponse getDataObject(GetDataObjectRequest request) { try { return getDataObjectAsync(request).toCompletableFuture().join(); } catch (CompletionException e) { - Throwable cause = e.getCause(); - if (cause instanceof InterruptedException) { - Thread.currentThread().interrupt(); - } - // Rethrow unchecked Exceptions - if (cause instanceof RuntimeException) { - throw (RuntimeException) cause; - } else { - throw new OpenSearchException(cause); - } + throw unwrapAndConvertToRuntime(e); } } @@ -120,16 +102,7 @@ default UpdateDataObjectResponse updateDataObject(UpdateDataObjectRequest reques try { return updateDataObjectAsync(request).toCompletableFuture().join(); } catch (CompletionException e) { - Throwable cause = e.getCause(); - if (cause instanceof InterruptedException) { - Thread.currentThread().interrupt(); - } - // Rethrow unchecked Exceptions - if (cause instanceof RuntimeException) { - throw (RuntimeException) cause; - } else { - throw new OpenSearchException(cause); - } + throw unwrapAndConvertToRuntime(e); } } @@ -159,16 +132,18 @@ default DeleteDataObjectResponse deleteDataObject(DeleteDataObjectRequest reques try { return deleteDataObjectAsync(request).toCompletableFuture().join(); } catch (CompletionException e) { - Throwable cause = e.getCause(); - if (cause instanceof InterruptedException) { - Thread.currentThread().interrupt(); - } - // Rethrow unchecked Exceptions - if (cause instanceof RuntimeException) { - throw (RuntimeException) cause; - } else { - throw new OpenSearchException(cause); - } + throw unwrapAndConvertToRuntime(e); } } + + private static RuntimeException unwrapAndConvertToRuntime(CompletionException e) { + Throwable cause = e.getCause(); + if (cause instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } + if (cause instanceof RuntimeException) { + return (RuntimeException) cause; + } + return new OpenSearchException(cause); + } }