Fix handling of ResponseException from legacy client dependency.

Original Pull Request #3146
Closes #3144

Signed-off-by: Peter-Josef Meisch <pj.meisch@sothawo.com>
This commit is contained in:
Peter-Josef Meisch 2025-08-04 20:50:47 +02:00 committed by GitHub
parent 006cda6de6
commit e49bb63df4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 223 additions and 220 deletions

View File

@ -24,6 +24,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseException;
import org.jspecify.annotations.Nullable;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
@ -33,6 +34,7 @@ import org.springframework.data.elasticsearch.NoSuchIndexException;
import org.springframework.data.elasticsearch.ResourceNotFoundException; import org.springframework.data.elasticsearch.ResourceNotFoundException;
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException; import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
import org.springframework.data.elasticsearch.VersionConflictException; import org.springframework.data.elasticsearch.VersionConflictException;
import org.springframework.util.ClassUtils;
/** /**
* Simple {@link PersistenceExceptionTranslator} for Elasticsearch. Convert the given runtime exception to an * Simple {@link PersistenceExceptionTranslator} for Elasticsearch. Convert the given runtime exception to an
@ -45,6 +47,9 @@ import org.springframework.data.elasticsearch.VersionConflictException;
*/ */
public class ElasticsearchExceptionTranslator implements PersistenceExceptionTranslator { public class ElasticsearchExceptionTranslator implements PersistenceExceptionTranslator {
public static final boolean LEGACY_RESTCLIENT_PRESENT = ClassUtils
.isPresent("org.elasticsearch.client.ResponseException", ElasticsearchExceptionTranslator.class.getClassLoader());
private final JsonpMapper jsonpMapper; private final JsonpMapper jsonpMapper;
public ElasticsearchExceptionTranslator(JsonpMapper jsonpMapper) { public ElasticsearchExceptionTranslator(JsonpMapper jsonpMapper) {
@ -68,7 +73,7 @@ public class ElasticsearchExceptionTranslator implements PersistenceExceptionTra
} }
@Override @Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) { public @Nullable DataAccessException translateExceptionIfPossible(RuntimeException ex) {
checkForConflictException(ex); checkForConflictException(ex);
@ -118,7 +123,7 @@ public class ElasticsearchExceptionTranslator implements PersistenceExceptionTra
Integer status = null; Integer status = null;
String message = null; String message = null;
if (exception instanceof ResponseException responseException) { if (LEGACY_RESTCLIENT_PRESENT && exception instanceof ResponseException responseException) {
// this code is for the old RestClient // this code is for the old RestClient
status = responseException.getResponse().getStatusLine().getStatusCode(); status = responseException.getResponse().getStatusLine().getStatusCode();
message = responseException.getMessage(); message = responseException.getMessage();

View File

@ -658,7 +658,7 @@ public class ElasticsearchTemplate extends AbstractElasticsearchTemplate {
QueryResponse response = sqlClient.query(requestConverter.sqlQueryRequest(query)); QueryResponse response = sqlClient.query(requestConverter.sqlQueryRequest(query));
return responseConverter.sqlResponse(response); return responseConverter.sqlResponse(response);
} catch (IOException e) { } catch (Exception e) {
throw exceptionTranslator.translateException(e); throw exceptionTranslator.translateException(e);
} }
} }

View File

@ -32,7 +32,6 @@ import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable; import org.jspecify.annotations.Nullable;
import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.support.HttpHeaders; import org.springframework.data.elasticsearch.support.HttpHeaders;
import org.springframework.data.elasticsearch.support.VersionInfo;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@ -46,8 +45,7 @@ public final class Rest5Clients {
public static final int DEFAULT_SOCKET_TIMEOUT_MILLIS = 30000; public static final int DEFAULT_SOCKET_TIMEOUT_MILLIS = 30000;
public static final int DEFAULT_RESPONSE_TIMEOUT_MILLIS = 0; // meaning infinite public static final int DEFAULT_RESPONSE_TIMEOUT_MILLIS = 0; // meaning infinite
private Rest5Clients() { private Rest5Clients() {}
}
/** /**
* Creates a low level {@link Rest5Client} for the given configuration. * Creates a low level {@link Rest5Client} for the given configuration.
@ -87,7 +85,6 @@ public final class Rest5Clients {
builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> { builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> {
httpAsyncClientBuilder.setUserAgent(VersionInfo.clientVersions());
if (clientConfiguration.getProxy().isPresent()) { if (clientConfiguration.getProxy().isPresent()) {
var proxy = clientConfiguration.getProxy().get(); var proxy = clientConfiguration.getProxy().get();
try { try {
@ -157,7 +154,8 @@ public final class Rest5Clients {
for (ClientConfiguration.ClientConfigurationCallback<?> clientConfigurer : clientConfiguration for (ClientConfiguration.ClientConfigurationCallback<?> clientConfigurer : clientConfiguration
.getClientConfigurers()) { .getClientConfigurers()) {
if (clientConfigurer instanceof ElasticsearchConnectionManagerCallback connectionManagerCallback) { if (clientConfigurer instanceof ElasticsearchConnectionManagerCallback connectionManagerCallback) {
poolingAsyncClientConnectionManagerBuilder = connectionManagerCallback.configure(poolingAsyncClientConnectionManagerBuilder); poolingAsyncClientConnectionManagerBuilder = connectionManagerCallback
.configure(poolingAsyncClientConnectionManagerBuilder);
} }
} }
}); });