Skip to content

Commit

Permalink
Move CompletionException handling to a common method
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Jun 14, 2024
1 parent bb7930e commit f02adc8
Showing 1 changed file with 15 additions and 40 deletions.
55 changes: 15 additions & 40 deletions common/src/main/java/org/opensearch/sdk/SdkClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

0 comments on commit f02adc8

Please sign in to comment.