Search in sources :

Example 1 with WriteConcernError

use of com.mongodb.bulk.WriteConcernError in project mongo-java-driver by mongodb.

the class WriteConcernHelper method createWriteConcernException.

@SuppressWarnings("deprecation")
public static MongoWriteConcernException createWriteConcernException(final BsonDocument result, final ServerAddress serverAddress) {
    MongoWriteConcernException writeConcernException = new MongoWriteConcernException(createWriteConcernError(result.getDocument("writeConcernError")), WriteConcernResult.acknowledged(0, false, null), serverAddress);
    result.getArray("errorLabels", new BsonArray()).stream().map(i -> i.asString().getValue()).forEach(writeConcernException::addLabel);
    return writeConcernException;
}
Also used : ServerAddress(com.mongodb.ServerAddress) ServerVersionHelper.serverIsAtLeastVersionThreeDotFour(com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotFour) MongoException(com.mongodb.MongoException) WriteConcernError(com.mongodb.bulk.WriteConcernError) CommandOperationHelper.addRetryableWriteErrorLabel(com.mongodb.internal.operation.CommandOperationHelper.addRetryableWriteErrorLabel) BsonString(org.bson.BsonString) ProtocolHelper(com.mongodb.internal.connection.ProtocolHelper) Collectors(java.util.stream.Collectors) BsonDocument(org.bson.BsonDocument) MongoWriteConcernException(com.mongodb.MongoWriteConcernException) ConnectionDescription(com.mongodb.connection.ConnectionDescription) BsonArray(org.bson.BsonArray) WriteConcernResult(com.mongodb.WriteConcernResult) WriteConcern(com.mongodb.WriteConcern) BsonArray(org.bson.BsonArray) MongoWriteConcernException(com.mongodb.MongoWriteConcernException)

Example 2 with WriteConcernError

use of com.mongodb.bulk.WriteConcernError 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 3 with WriteConcernError

use of com.mongodb.bulk.WriteConcernError in project spring-data-mongodb by spring-projects.

the class DefaultBulkOperationsUnitTests method translateMongoBulkOperationExceptionWithWriteConcernError.

// DATAMONGO-2285
@Test
public void translateMongoBulkOperationExceptionWithWriteConcernError() {
    when(collection.bulkWrite(anyList(), any(BulkWriteOptions.class))).thenThrow(new MongoBulkWriteException(null, Collections.emptyList(), new WriteConcernError(42, "codename", "writeconcern error happened", new BsonDocument()), new ServerAddress()));
    assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy(() -> ops.insert(new SomeDomainType()).execute());
}
Also used : BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) BsonDocument(org.bson.BsonDocument) WriteConcernError(com.mongodb.bulk.WriteConcernError) ServerAddress(com.mongodb.ServerAddress) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.jupiter.api.Test)

Example 4 with WriteConcernError

use of com.mongodb.bulk.WriteConcernError in project mongo-java-driver by mongodb.

the class BaseWriteOperation method manufactureGetLastErrorResponse.

private BsonDocument manufactureGetLastErrorResponse(final MongoBulkWriteException e) {
    BsonDocument response = new BsonDocument();
    addBulkWriteResultToResponse(e.getWriteResult(), response);
    WriteConcernError writeConcernError = e.getWriteConcernError();
    if (writeConcernError != null) {
        response.putAll(writeConcernError.getDetails());
    }
    BulkWriteError lastError = getLastError(e);
    if (lastError != null) {
        response.put("err", new BsonString(lastError.getMessage()));
        response.put("code", new BsonInt32(lastError.getCode()));
        response.putAll(lastError.getDetails());
    } else if (writeConcernError != null) {
        response.put("err", new BsonString(writeConcernError.getMessage()));
        response.put("code", new BsonInt32(writeConcernError.getCode()));
    }
    return response;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) WriteConcernError(com.mongodb.bulk.WriteConcernError) BulkWriteError(com.mongodb.bulk.BulkWriteError)

Aggregations

WriteConcernError (com.mongodb.bulk.WriteConcernError)4 BsonDocument (org.bson.BsonDocument)4 MongoException (com.mongodb.MongoException)2 MongoWriteConcernException (com.mongodb.MongoWriteConcernException)2 ServerAddress (com.mongodb.ServerAddress)2 WriteConcernResult (com.mongodb.WriteConcernResult)2 BsonString (org.bson.BsonString)2 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1 MongoWriteException (com.mongodb.MongoWriteException)1 WriteConcern (com.mongodb.WriteConcern)1 WriteError (com.mongodb.WriteError)1 BulkWriteError (com.mongodb.bulk.BulkWriteError)1 BulkWriteOptions (com.mongodb.client.model.BulkWriteOptions)1 ConnectionDescription (com.mongodb.connection.ConnectionDescription)1 ProtocolHelper (com.mongodb.internal.connection.ProtocolHelper)1 CommandOperationHelper.addRetryableWriteErrorLabel (com.mongodb.internal.operation.CommandOperationHelper.addRetryableWriteErrorLabel)1 ServerVersionHelper.serverIsAtLeastVersionThreeDotFour (com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotFour)1 Collectors (java.util.stream.Collectors)1 BsonArray (org.bson.BsonArray)1 BsonInt32 (org.bson.BsonInt32)1