Search in sources :

Example 1 with UncategorizedMongoDbException

use of org.springframework.data.mongodb.UncategorizedMongoDbException in project spring-data-mongodb by spring-projects.

the class MongoPersistentEntityIndexCreator method createIndex.

void createIndex(IndexDefinitionHolder indexDefinition) {
    try {
        IndexOperations indexOperations = indexOperationsProvider.indexOps(indexDefinition.getCollection());
        indexOperations.ensureIndex(indexDefinition);
    } catch (UncategorizedMongoDbException ex) {
        if (ex.getCause() instanceof MongoException && MongoDbErrorCodes.isDataIntegrityViolationCode(((MongoException) ex.getCause()).getCode())) {
            IndexInfo existingIndex = fetchIndexInformation(indexDefinition);
            String message = "Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'.";
            if (existingIndex != null) {
                message += " Index already defined as '%s'.";
            }
            throw new DataIntegrityViolationException(String.format(message, indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions(), existingIndex), ex.getCause());
        }
        throw ex;
    }
}
Also used : MongoException(com.mongodb.MongoException) UncategorizedMongoDbException(org.springframework.data.mongodb.UncategorizedMongoDbException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Aggregations

MongoException (com.mongodb.MongoException)1 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)1 UncategorizedMongoDbException (org.springframework.data.mongodb.UncategorizedMongoDbException)1