Skip to content

Commit

Permalink
Sonarqube Fix - Ensure optional is present before get (#1233)
Browse files Browse the repository at this point in the history
* Ensure optional is present before `get`
* Add specific exception when instantiating a client fails

Signed-off-by: Mikayla Thompson <[email protected]>
  • Loading branch information
mikaylathompson authored Jan 15, 2025
1 parent 37f955c commit 8d1678e
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public OpenSearchClient determineVersionAndCreate() {
return clientClass.getConstructor(ConnectionContext.class, Version.class)
.newInstance(connectionContext, version);
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Failed to instantiate OpenSearchClient", e);
throw new ClientInstantiationException("Failed to instantiate OpenSearchClient", e);
}
}

Expand All @@ -59,7 +59,7 @@ public OpenSearchClient determineVersionAndCreate(RestClient restClient, FailedR
return clientClass.getConstructor(RestClient.class, FailedRequestsLogger.class, Version.class)
.newInstance(restClient, failedRequestsLogger, version);
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Failed to instantiate OpenSearchClient", e);
throw new ClientInstantiationException("Failed to instantiate OpenSearchClient", e);
}
}

Expand Down Expand Up @@ -191,9 +191,7 @@ private Mono<Version> getVersionFromNodes(HttpResponse resp) {

if (foundVersions.isEmpty()) {
return Mono.error(new OpenSearchClient.OperationFailed("Unable to find any version numbers", resp));
}

if (foundVersions.size() == 1) {
} else if (foundVersions.size() == 1) {
return Mono.just(foundVersions.stream().findFirst().get());
}

Expand All @@ -209,5 +207,10 @@ private Flavor getLikelyOpenSearchFlavor() {
return client.getConnectionContext().isAwsSpecificAuthentication() ? Flavor.AMAZON_MANAGED_OPENSEARCH : Flavor.OPENSEARCH;
}

public static class ClientInstantiationException extends RuntimeException {
public ClientInstantiationException(String message, Exception cause) {
super(message, cause);
}
}

}

0 comments on commit 8d1678e

Please sign in to comment.