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;
}
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;
});
}
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());
}
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;
}
Aggregations