Search in sources :

Example 1 with WriteError

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;
    });
}
Also used : WriteError(com.mongodb.WriteError) WriteConcernResult(com.mongodb.WriteConcernResult) MongoException(com.mongodb.MongoException) BsonDocument(org.bson.BsonDocument) MongoWriteException(com.mongodb.MongoWriteException) WriteConcernError(com.mongodb.bulk.WriteConcernError) MongoWriteConcernException(com.mongodb.MongoWriteConcernException)

Example 2 with WriteError

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));
}
Also used : BeforeSaveEvent(org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent) MongoWriteException(com.mongodb.MongoWriteException) BeforeConvertEvent(org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent) WriteError(com.mongodb.WriteError) BulkWriteError(com.mongodb.bulk.BulkWriteError) BulkOperationContext(org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) UpdateMapper(org.springframework.data.mongodb.core.convert.UpdateMapper) Test(org.junit.jupiter.api.Test)

Aggregations

MongoWriteException (com.mongodb.MongoWriteException)2 WriteError (com.mongodb.WriteError)2 BsonDocument (org.bson.BsonDocument)2 MongoException (com.mongodb.MongoException)1 MongoWriteConcernException (com.mongodb.MongoWriteConcernException)1 WriteConcernResult (com.mongodb.WriteConcernResult)1 BulkWriteError (com.mongodb.bulk.BulkWriteError)1 WriteConcernError (com.mongodb.bulk.WriteConcernError)1 BulkWriteOptions (com.mongodb.client.model.BulkWriteOptions)1 BsonString (org.bson.BsonString)1 Test (org.junit.jupiter.api.Test)1 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)1 BulkOperationContext (org.springframework.data.mongodb.core.DefaultBulkOperations.BulkOperationContext)1 QueryMapper (org.springframework.data.mongodb.core.convert.QueryMapper)1 UpdateMapper (org.springframework.data.mongodb.core.convert.UpdateMapper)1 BeforeConvertEvent (org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent)1 BeforeSaveEvent (org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent)1