Skip to content

Commit e49bb63

Browse files
authored
Fix handling of ResponseException from legacy client dependency.
Original Pull Request #3146 Closes #3144 Signed-off-by: Peter-Josef Meisch <pj.meisch@sothawo.com>
1 parent 006cda6 commit e49bb63

File tree

3 files changed

+271
-268
lines changed

3 files changed

+271
-268
lines changed

src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchExceptionTranslator.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.regex.Pattern;
2525

2626
import org.elasticsearch.client.ResponseException;
27+
import org.jspecify.annotations.Nullable;
2728
import org.springframework.dao.DataAccessException;
2829
import org.springframework.dao.DataAccessResourceFailureException;
2930
import org.springframework.dao.DataIntegrityViolationException;
@@ -33,6 +34,7 @@
3334
import org.springframework.data.elasticsearch.ResourceNotFoundException;
3435
import org.springframework.data.elasticsearch.UncategorizedElasticsearchException;
3536
import org.springframework.data.elasticsearch.VersionConflictException;
37+
import org.springframework.util.ClassUtils;
3638

3739
/**
3840
* Simple {@link PersistenceExceptionTranslator} for Elasticsearch. Convert the given runtime exception to an
@@ -45,6 +47,9 @@
4547
*/
4648
public class ElasticsearchExceptionTranslator implements PersistenceExceptionTranslator {
4749

50+
public static final boolean LEGACY_RESTCLIENT_PRESENT = ClassUtils
51+
.isPresent("org.elasticsearch.client.ResponseException", ElasticsearchExceptionTranslator.class.getClassLoader());
52+
4853
private final JsonpMapper jsonpMapper;
4954

5055
public ElasticsearchExceptionTranslator(JsonpMapper jsonpMapper) {
@@ -68,7 +73,7 @@ public RuntimeException translateException(Throwable throwable) {
6873
}
6974

7075
@Override
71-
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
76+
public @Nullable DataAccessException translateExceptionIfPossible(RuntimeException ex) {
7277

7378
checkForConflictException(ex);
7479

@@ -118,7 +123,7 @@ private void checkForConflictException(Throwable exception) {
118123
Integer status = null;
119124
String message = null;
120125

121-
if (exception instanceof ResponseException responseException) {
126+
if (LEGACY_RESTCLIENT_PRESENT && exception instanceof ResponseException responseException) {
122127
// this code is for the old RestClient
123128
status = responseException.getResponse().getStatusLine().getStatusCode();
124129
message = responseException.getMessage();

src/main/java/org/springframework/data/elasticsearch/client/elc/ElasticsearchTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ public SqlResponse search(SqlQuery query) {
658658
QueryResponse response = sqlClient.query(requestConverter.sqlQueryRequest(query));
659659

660660
return responseConverter.sqlResponse(response);
661-
} catch (IOException e) {
661+
} catch (Exception e) {
662662
throw exceptionTranslator.translateException(e);
663663
}
664664
}

0 commit comments

Comments
 (0)