use of com.mongodb.WriteError in project mongo-java-driver by mongodb.
the class MongoOperationPublisher method createSingleWriteRequestMono.
private Mono<BulkWriteResult> createSingleWriteRequestMono(final Supplier<AsyncWriteOperation<BulkWriteResult>> operation, @Nullable final ClientSession clientSession, final WriteRequest.Type type) {
return createWriteOperationMono(operation, clientSession).onErrorMap(MongoBulkWriteException.class, e -> {
MongoException exception;
WriteConcernError writeConcernError = e.getWriteConcernError();
if (e.getWriteErrors().isEmpty() && writeConcernError != null) {
WriteConcernResult writeConcernResult;
if (type == WriteRequest.Type.INSERT) {
writeConcernResult = WriteConcernResult.acknowledged(e.getWriteResult().getInsertedCount(), false, null);
} else if (type == WriteRequest.Type.DELETE) {
writeConcernResult = WriteConcernResult.acknowledged(e.getWriteResult().getDeletedCount(), false, null);
} else {
writeConcernResult = WriteConcernResult.acknowledged(e.getWriteResult().getMatchedCount() + e.getWriteResult().getUpserts().size(), e.getWriteResult().getMatchedCount() > 0, e.getWriteResult().getUpserts().isEmpty() ? null : e.getWriteResult().getUpserts().get(0).getId());
}
exception = new MongoWriteConcernException(writeConcernError, writeConcernResult, e.getServerAddress());
} else if (!e.getWriteErrors().isEmpty()) {
exception = new MongoWriteException(new WriteError(e.getWriteErrors().get(0)), e.getServerAddress());
} else {
exception = new MongoWriteException(new WriteError(-1, "Unknown write error", new BsonDocument()), e.getServerAddress());
}
for (final String errorLabel : e.getErrorLabels()) {
exception.addLabel(errorLabel);
}
return exception;
});
}
use of com.mongodb.WriteError in project spring-data-mongodb by spring-projects.
the class DefaultBulkOperationsUnitTests method noAfterSaveEventOnFailure.
// DATAMONGO-2290
@Test
void noAfterSaveEventOnFailure() {
ApplicationEventPublisher eventPublisher = mock(ApplicationEventPublisher.class);
when(collection.bulkWrite(anyList(), any(BulkWriteOptions.class))).thenThrow(new MongoWriteException(new WriteError(89, "NetworkTimeout", new BsonDocument("hi", new BsonString("there!"))), null));
ops = new DefaultBulkOperations(template, "collection-1", new BulkOperationContext(BulkMode.ORDERED, Optional.of(mappingContext.getPersistentEntity(Person.class)), new QueryMapper(converter), new UpdateMapper(converter), eventPublisher, null));
ops.insert(new SomeDomainType());
verify(eventPublisher).publishEvent(any(BeforeConvertEvent.class));
try {
ops.execute();
fail("Missing MongoWriteException");
} catch (MongoWriteException expected) {
}
verify(eventPublisher).publishEvent(any(BeforeSaveEvent.class));
}
Aggregations