24
24
import java .util .regex .Pattern ;
25
25
26
26
import org .elasticsearch .client .ResponseException ;
27
+ import org .jspecify .annotations .Nullable ;
27
28
import org .springframework .dao .DataAccessException ;
28
29
import org .springframework .dao .DataAccessResourceFailureException ;
29
30
import org .springframework .dao .DataIntegrityViolationException ;
33
34
import org .springframework .data .elasticsearch .ResourceNotFoundException ;
34
35
import org .springframework .data .elasticsearch .UncategorizedElasticsearchException ;
35
36
import org .springframework .data .elasticsearch .VersionConflictException ;
37
+ import org .springframework .util .ClassUtils ;
36
38
37
39
/**
38
40
* Simple {@link PersistenceExceptionTranslator} for Elasticsearch. Convert the given runtime exception to an
45
47
*/
46
48
public class ElasticsearchExceptionTranslator implements PersistenceExceptionTranslator {
47
49
50
+ public static final boolean LEGACY_RESTCLIENT_PRESENT = ClassUtils
51
+ .isPresent ("org.elasticsearch.client.ResponseException" , ElasticsearchExceptionTranslator .class .getClassLoader ());
52
+
48
53
private final JsonpMapper jsonpMapper ;
49
54
50
55
public ElasticsearchExceptionTranslator (JsonpMapper jsonpMapper ) {
@@ -68,7 +73,7 @@ public RuntimeException translateException(Throwable throwable) {
68
73
}
69
74
70
75
@ Override
71
- public DataAccessException translateExceptionIfPossible (RuntimeException ex ) {
76
+ public @ Nullable DataAccessException translateExceptionIfPossible (RuntimeException ex ) {
72
77
73
78
checkForConflictException (ex );
74
79
@@ -118,7 +123,7 @@ private void checkForConflictException(Throwable exception) {
118
123
Integer status = null ;
119
124
String message = null ;
120
125
121
- if (exception instanceof ResponseException responseException ) {
126
+ if (LEGACY_RESTCLIENT_PRESENT && exception instanceof ResponseException responseException ) {
122
127
// this code is for the old RestClient
123
128
status = responseException .getResponse ().getStatusLine ().getStatusCode ();
124
129
message = responseException .getMessage ();
0 commit comments