20
20
import co .elastic .clients .json .JsonpMapper ;
21
21
22
22
import java .io .IOException ;
23
+ import java .util .regex .Matcher ;
24
+ import java .util .regex .Pattern ;
23
25
26
+ import org .elasticsearch .client .ResponseException ;
24
27
import org .springframework .dao .DataAccessException ;
25
28
import org .springframework .dao .DataAccessResourceFailureException ;
29
+ import org .springframework .dao .DataIntegrityViolationException ;
26
30
import org .springframework .dao .OptimisticLockingFailureException ;
27
31
import org .springframework .dao .support .PersistenceExceptionTranslator ;
28
- import org .springframework .data .elasticsearch .RestStatusException ;
32
+ import org .springframework .data .elasticsearch .NoSuchIndexException ;
29
33
import org .springframework .data .elasticsearch .UncategorizedElasticsearchException ;
34
+ import org .springframework .http .HttpStatus ;
30
35
31
36
/**
32
37
* Simple {@link PersistenceExceptionTranslator} for Elasticsearch. Convert the given runtime exception to an
@@ -67,14 +72,28 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
67
72
return new OptimisticLockingFailureException ("Cannot index a document due to seq_no+primary_term conflict" , ex );
68
73
}
69
74
70
- // todo #1973 index unavailable?
71
-
72
75
if (ex instanceof ElasticsearchException ) {
73
76
ElasticsearchException elasticsearchException = (ElasticsearchException ) ex ;
74
77
75
78
ErrorResponse response = elasticsearchException .response ();
79
+
80
+ if (response .status () == HttpStatus .NOT_FOUND .value ()
81
+ && "index_not_found_exception" .equals (response .error ().type ())) {
82
+
83
+ Pattern pattern = Pattern .compile (".*no such index \\ [(.*)\\ ]" );
84
+ String index = "" ;
85
+ Matcher matcher = pattern .matcher (response .error ().reason ());
86
+ if (matcher .matches ()) {
87
+ index = matcher .group (1 );
88
+ }
89
+ return new NoSuchIndexException (index );
90
+ }
76
91
String body = JsonUtils .toJson (response , jsonpMapper );
77
92
93
+ if (response .error ().type ().contains ("validation_exception" )) {
94
+ return new DataIntegrityViolationException (response .error ().reason ());
95
+ }
96
+
78
97
return new UncategorizedElasticsearchException (ex .getMessage (), response .status (), body , ex );
79
98
}
80
99
@@ -86,20 +105,22 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
86
105
return null ;
87
106
}
88
107
89
- private boolean isSeqNoConflict (Exception exception ) {
108
+ private boolean isSeqNoConflict (Throwable exception ) {
90
109
// todo #1973 check if this works
91
110
Integer status = null ;
92
111
String message = null ;
93
112
94
- if (exception instanceof RestStatusException ) {
95
113
96
- RestStatusException statusException = (RestStatusException ) exception ;
97
- status = statusException .getStatus ();
98
- message = statusException .getMessage ();
114
+ if (exception instanceof ResponseException ) {
115
+ ResponseException responseException = (ResponseException ) exception ;
116
+ status = responseException .getResponse ().getStatusLine ().getStatusCode ();
117
+ message = responseException .getMessage ();
118
+ } else if (exception .getCause () != null ) {
119
+ return isSeqNoConflict (exception .getCause ());
99
120
}
100
121
101
122
if (status != null && message != null ) {
102
- return status == 409 && message .contains ("type= version_conflict_engine_exception" )
123
+ return status == 409 && message .contains ("type\" : \" version_conflict_engine_exception" )
103
124
&& message .contains ("version conflict, required seqNo" );
104
125
}
105
126
0 commit comments