21
21
import java .util .Set ;
22
22
23
23
import org .bson .BsonInvalidOperationException ;
24
-
25
24
import org .springframework .dao .DataAccessException ;
26
25
import org .springframework .dao .DataAccessResourceFailureException ;
27
26
import org .springframework .dao .DataIntegrityViolationException ;
32
31
import org .springframework .dao .TransientDataAccessException ;
33
32
import org .springframework .dao .support .PersistenceExceptionTranslator ;
34
33
import org .springframework .data .mongodb .ClientSessionException ;
35
- import org .springframework .data .mongodb .MongoTransactionException ;
36
34
import org .springframework .data .mongodb .TransientClientSessionException ;
37
35
import org .springframework .data .mongodb .TransientMongoDbException ;
38
- import org .springframework .data .mongodb .TransientMongoDbTransactionException ;
39
36
import org .springframework .data .mongodb .UncategorizedMongoDbException ;
40
37
import org .springframework .data .mongodb .util .MongoDbErrorCodes ;
41
38
import org .springframework .lang .Nullable ;
42
39
import org .springframework .util .ClassUtils ;
43
40
44
41
import com .mongodb .MongoBulkWriteException ;
45
42
import com .mongodb .MongoException ;
46
- import com .mongodb .MongoServerException ;
47
43
import com .mongodb .MongoSocketException ;
48
44
import com .mongodb .bulk .BulkWriteError ;
49
45
@@ -87,7 +83,8 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
87
83
// Translated exceptions that per se are not be recoverable (eg. WriteConflicts), might still be transient inside a
88
84
// transaction. Let's wrap those.
89
85
return (isTransientFailure (ex ) && !(translatedException instanceof TransientDataAccessException ))
90
- ? new TransientMongoDbException (ex .getMessage (), translatedException ) : translatedException ;
86
+ ? new TransientMongoDbException (ex .getMessage (), translatedException )
87
+ : translatedException ;
91
88
92
89
}
93
90
@@ -120,13 +117,13 @@ DataAccessException doTranslateException(RuntimeException ex) {
120
117
121
118
if (DATA_INTEGRITY_EXCEPTIONS .contains (exception )) {
122
119
123
- if (ex instanceof MongoServerException ) {
124
- if ((( MongoServerException ) ex ). getCode () == 11000 ) {
120
+ if (ex instanceof MongoException ) {
121
+ if (MongoDbErrorCodes . isDataDuplicateKeyError ( ex )) {
125
122
return new DuplicateKeyException (ex .getMessage (), ex );
126
123
}
127
124
if (ex instanceof MongoBulkWriteException ) {
128
- for (BulkWriteError x : ((MongoBulkWriteException ) ex ).getWriteErrors ()) {
129
- if (x . getCode () == 11000 ) {
125
+ for (BulkWriteError writeError : ((MongoBulkWriteException ) ex ).getWriteErrors ()) {
126
+ if (MongoDbErrorCodes . isDuplicateKeyCode ( writeError . getCode ()) ) {
130
127
return new DuplicateKeyException (ex .getMessage (), ex );
131
128
}
132
129
}
@@ -140,24 +137,30 @@ DataAccessException doTranslateException(RuntimeException ex) {
140
137
if (ex instanceof MongoException ) {
141
138
142
139
MongoException mongoException = (MongoException ) ex ;
143
- int code = mongoException .getCode ();
144
- boolean isTransient = isTransientFailure (mongoException );
145
140
146
- if (MongoDbErrorCodes .isDuplicateKeyCode ( code )) {
141
+ if (MongoDbErrorCodes .isDuplicateKeyError ( mongoException )) {
147
142
return new DuplicateKeyException (ex .getMessage (), ex );
148
- } else if (MongoDbErrorCodes .isDataAccessResourceFailureCode (code )) {
143
+ }
144
+ if (MongoDbErrorCodes .isDataAccessResourceError (mongoException )) {
149
145
return new DataAccessResourceFailureException (ex .getMessage (), ex );
150
- } else if (MongoDbErrorCodes .isInvalidDataAccessApiUsageCode (code ) || code == 10003 || code == 12001
151
- || code == 12010 || code == 12011 || code == 12012 ) {
152
- return new InvalidDataAccessApiUsageException (ex .getMessage (), ex );
153
- } else if (MongoDbErrorCodes .isPermissionDeniedCode (code )) {
146
+ }
147
+
148
+ {
149
+ int code = mongoException .getCode ();
150
+ if (MongoDbErrorCodes .isInvalidDataAccessApiUsageError (mongoException ) || code == 12001 || code == 12010
151
+ || code == 12011 || code == 12012 ) {
152
+ return new InvalidDataAccessApiUsageException (ex .getMessage (), ex );
153
+ }
154
+ }
155
+ if (MongoDbErrorCodes .isPermissionDeniedError (mongoException )) {
154
156
return new PermissionDeniedDataAccessException (ex .getMessage (), ex );
155
- } else if (MongoDbErrorCodes .isClientSessionFailureCode (code )) {
156
- return isTransient ? new TransientClientSessionException (ex .getMessage (), ex )
157
+ }
158
+ if (MongoDbErrorCodes .isDataIntegrityViolationError (mongoException )) {
159
+ return new DataIntegrityViolationException (mongoException .getMessage (), mongoException );
160
+ }
161
+ if (MongoDbErrorCodes .isClientSessionFailure (mongoException )) {
162
+ return isTransientFailure (mongoException ) ? new TransientClientSessionException (ex .getMessage (), ex )
157
163
: new ClientSessionException (ex .getMessage (), ex );
158
- } else if (MongoDbErrorCodes .isTransactionFailureCode (code )) {
159
- return isTransient ? new TransientMongoDbTransactionException (ex .getMessage (), ex )
160
- : new MongoTransactionException (ex .getMessage (), ex );
161
164
}
162
165
163
166
return new UncategorizedMongoDbException (ex .getMessage (), ex );
@@ -186,7 +189,7 @@ DataAccessException doTranslateException(RuntimeException ex) {
186
189
* @return {@literal true} if the given {@link Exception} is a {@link MongoException} holding one of the transient
187
190
* exception error labels.
188
191
* @see MongoException#hasErrorLabel(String)
189
- * @since 2.1
192
+ * @since 3.3
190
193
*/
191
194
public static boolean isTransientFailure (Exception e ) {
192
195
0 commit comments