Skip to content

Commit 7e7c37d

Browse files
Don't forget to translate WriteErrors to DataIntegrityViolationException.
Should not have been an UncategorizedMongoDbException in first place.
1 parent 9a2bc7d commit 7e7c37d

File tree

8 files changed

+141
-127
lines changed

8 files changed

+141
-127
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/TransientClientSessionException.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,10 @@
2323
* access failures such as reading data using an already closed session.
2424
*
2525
* @author Christoph Strobl
26-
* @since 2.1
26+
* @since 3.3
2727
*/
2828
public class TransientClientSessionException extends TransientMongoDbException {
2929

30-
/**
31-
* Constructor for {@link TransientClientSessionException}.
32-
*
33-
* @param msg the detail message. Must not be {@literal null}.
34-
*/
35-
public TransientClientSessionException(String msg) {
36-
super(msg);
37-
}
38-
3930
/**
4031
* Constructor for {@link TransientClientSessionException}.
4132
*

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/TransientMongoDbException.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,10 @@
2424
* specific labels}.
2525
*
2626
* @author Christoph Strobl
27-
* @since 2.1
27+
* @since 3.3
2828
*/
2929
public class TransientMongoDbException extends TransientDataAccessException {
3030

31-
/**
32-
* Constructor for {@link TransientMongoDbException}.
33-
*
34-
* @param msg the detail message. Must not be {@literal null}.
35-
*/
36-
public TransientMongoDbException(String msg) {
37-
super(msg);
38-
}
39-
4031
/**
4132
* Constructor for {@link TransientMongoDbException}.
4233
*

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/TransientMongoDbTransactionException.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoExceptionTranslator.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Set;
2222

2323
import org.bson.BsonInvalidOperationException;
24-
2524
import org.springframework.dao.DataAccessException;
2625
import org.springframework.dao.DataAccessResourceFailureException;
2726
import org.springframework.dao.DataIntegrityViolationException;
@@ -32,18 +31,15 @@
3231
import org.springframework.dao.TransientDataAccessException;
3332
import org.springframework.dao.support.PersistenceExceptionTranslator;
3433
import org.springframework.data.mongodb.ClientSessionException;
35-
import org.springframework.data.mongodb.MongoTransactionException;
3634
import org.springframework.data.mongodb.TransientClientSessionException;
3735
import org.springframework.data.mongodb.TransientMongoDbException;
38-
import org.springframework.data.mongodb.TransientMongoDbTransactionException;
3936
import org.springframework.data.mongodb.UncategorizedMongoDbException;
4037
import org.springframework.data.mongodb.util.MongoDbErrorCodes;
4138
import org.springframework.lang.Nullable;
4239
import org.springframework.util.ClassUtils;
4340

4441
import com.mongodb.MongoBulkWriteException;
4542
import com.mongodb.MongoException;
46-
import com.mongodb.MongoServerException;
4743
import com.mongodb.MongoSocketException;
4844
import com.mongodb.bulk.BulkWriteError;
4945

@@ -87,7 +83,8 @@ public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
8783
// Translated exceptions that per se are not be recoverable (eg. WriteConflicts), might still be transient inside a
8884
// transaction. Let's wrap those.
8985
return (isTransientFailure(ex) && !(translatedException instanceof TransientDataAccessException))
90-
? new TransientMongoDbException(ex.getMessage(), translatedException) : translatedException;
86+
? new TransientMongoDbException(ex.getMessage(), translatedException)
87+
: translatedException;
9188

9289
}
9390

@@ -120,13 +117,13 @@ DataAccessException doTranslateException(RuntimeException ex) {
120117

121118
if (DATA_INTEGRITY_EXCEPTIONS.contains(exception)) {
122119

123-
if (ex instanceof MongoServerException) {
124-
if (((MongoServerException) ex).getCode() == 11000) {
120+
if (ex instanceof MongoException) {
121+
if (MongoDbErrorCodes.isDataDuplicateKeyError(ex)) {
125122
return new DuplicateKeyException(ex.getMessage(), ex);
126123
}
127124
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())) {
130127
return new DuplicateKeyException(ex.getMessage(), ex);
131128
}
132129
}
@@ -140,24 +137,30 @@ DataAccessException doTranslateException(RuntimeException ex) {
140137
if (ex instanceof MongoException) {
141138

142139
MongoException mongoException = (MongoException) ex;
143-
int code = mongoException.getCode();
144-
boolean isTransient = isTransientFailure(mongoException);
145140

146-
if (MongoDbErrorCodes.isDuplicateKeyCode(code)) {
141+
if (MongoDbErrorCodes.isDuplicateKeyError(mongoException)) {
147142
return new DuplicateKeyException(ex.getMessage(), ex);
148-
} else if (MongoDbErrorCodes.isDataAccessResourceFailureCode(code)) {
143+
}
144+
if (MongoDbErrorCodes.isDataAccessResourceError(mongoException)) {
149145
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)) {
154156
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)
157163
: 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);
161164
}
162165

163166
return new UncategorizedMongoDbException(ex.getMessage(), ex);
@@ -186,7 +189,7 @@ DataAccessException doTranslateException(RuntimeException ex) {
186189
* @return {@literal true} if the given {@link Exception} is a {@link MongoException} holding one of the transient
187190
* exception error labels.
188191
* @see MongoException#hasErrorLabel(String)
189-
* @since 2.1
192+
* @since 3.3
190193
*/
191194
public static boolean isTransientFailure(Exception e) {
192195

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.data.mapping.context.MappingContext;
2828
import org.springframework.data.mapping.context.MappingContextEvent;
2929
import org.springframework.data.mongodb.MongoDatabaseFactory;
30-
import org.springframework.data.mongodb.UncategorizedMongoDbException;
3130
import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.IndexDefinitionHolder;
3231
import org.springframework.data.mongodb.core.mapping.Document;
3332
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
@@ -155,7 +154,7 @@ void createIndex(IndexDefinitionHolder indexDefinition) {
155154
IndexOperations indexOperations = indexOperationsProvider.indexOps(indexDefinition.getCollection());
156155
indexOperations.ensureIndex(indexDefinition);
157156

158-
} catch (UncategorizedMongoDbException ex) {
157+
} catch (DataIntegrityViolationException ex) {
159158

160159
if (ex.getCause() instanceof MongoException
161160
&& MongoDbErrorCodes.isDataIntegrityViolationCode(((MongoException) ex.getCause()).getCode())) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/IndexEnsuringQueryCreationListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import org.slf4j.Logger;
2424
import org.slf4j.LoggerFactory;
2525
import org.springframework.core.annotation.AnnotatedElementUtils;
26+
import org.springframework.dao.DataIntegrityViolationException;
2627
import org.springframework.data.domain.Sort;
2728
import org.springframework.data.domain.Sort.Direction;
2829
import org.springframework.data.domain.Sort.Order;
29-
import org.springframework.data.mongodb.UncategorizedMongoDbException;
3030
import org.springframework.data.mongodb.core.MongoOperations;
3131
import org.springframework.data.mongodb.core.index.Index;
3232
import org.springframework.data.mongodb.core.index.IndexOperationsProvider;
@@ -117,7 +117,7 @@ public void onCreation(PartTreeMongoQuery query) {
117117
MongoEntityMetadata<?> metadata = query.getQueryMethod().getEntityInformation();
118118
try {
119119
indexOperationsProvider.indexOps(metadata.getCollectionName(), metadata.getJavaType()).ensureIndex(index);
120-
} catch (UncategorizedMongoDbException e) {
120+
} catch (DataIntegrityViolationException e) {
121121

122122
if (e.getCause() instanceof MongoException) {
123123

0 commit comments

Comments
 (0)