private Mono<? extends String> translateException(Throwable e, IndexDefinitionHolder indexDefinition) { Mono<IndexInfo> existingIndex = fetchIndexInformation(indexDefinition); Mono<String> defaultError = Mono.error(new DataIntegrityViolationException( String.format("Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'.", indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions()), e.getCause())); return existingIndex.flatMap(it -> { return Mono.<String> error(new DataIntegrityViolationException( String.format("Index already defined as '%s'.", indexDefinition.getPath()), e.getCause())); }).switchIfEmpty(defaultError); }
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; } }
private Mono<? extends String> translateException(Throwable e, IndexDefinitionHolder indexDefinition) { Mono<IndexInfo> existingIndex = fetchIndexInformation(indexDefinition); Mono<String> defaultError = Mono.error(new DataIntegrityViolationException( String.format("Cannot create index for '%s' in collection '%s' with keys '%s' and options '%s'.", indexDefinition.getPath(), indexDefinition.getCollection(), indexDefinition.getIndexKeys(), indexDefinition.getIndexOptions()), e.getCause())); return existingIndex.flatMap(it -> { return Mono.<String> error(new DataIntegrityViolationException( String.format("Index already defined as '%s'.", indexDefinition.getPath()), e.getCause())); }).switchIfEmpty(defaultError); }
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; } }