use of com.mongodb.MongoWriteConcernException in project mongo-java-driver by mongodb.
the class FindAndModifyHelper method transformDocument.
@SuppressWarnings("deprecation")
private static <T> T transformDocument(final BsonDocument result, final ServerAddress serverAddress) {
if (hasWriteConcernError(result)) {
MongoWriteConcernException writeConcernException = new MongoWriteConcernException(createWriteConcernError(result.getDocument("writeConcernError")), createWriteConcernResult(result.getDocument("lastErrorObject", new BsonDocument())), serverAddress);
result.getArray("errorLabels", new BsonArray()).stream().map(i -> i.asString().getValue()).forEach(writeConcernException::addLabel);
throw writeConcernException;
}
if (!result.isDocument("value")) {
return null;
}
return BsonDocumentWrapperHelper.toDocument(result.getDocument("value", null));
}
use of com.mongodb.MongoWriteConcernException 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.MongoWriteConcernException 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.MongoWriteConcernException in project mongo-java-driver by mongodb.
the class AbstractUnifiedTest method assertExceptionState.
private boolean assertExceptionState(final RuntimeException e, final BsonValue expectedResult, final String operationName) {
boolean passedAssertion = false;
if (hasErrorLabelsContainField(expectedResult)) {
if (e instanceof MongoException) {
MongoException mongoException = (MongoException) e;
for (String curErrorLabel : getErrorLabelsContainField(expectedResult)) {
assertTrue(String.format("Expected error label '%s but found labels '%s' for operation %s", curErrorLabel, mongoException.getErrorLabels(), operationName), mongoException.hasErrorLabel(curErrorLabel));
}
passedAssertion = true;
}
}
if (hasErrorLabelsOmitField(expectedResult)) {
if (e instanceof MongoException) {
MongoException mongoException = (MongoException) e;
for (String curErrorLabel : getErrorLabelsOmitField(expectedResult)) {
assertFalse(String.format("Expected error label '%s omitted but found labels '%s' for operation %s", curErrorLabel, mongoException.getErrorLabels(), operationName), mongoException.hasErrorLabel(curErrorLabel));
}
passedAssertion = true;
}
}
if (hasErrorContainsField(expectedResult)) {
String expectedError = getErrorContainsField(expectedResult);
assertTrue(String.format("Expected '%s' but got '%s' for operation %s", expectedError, e.getMessage(), operationName), e.getMessage().toLowerCase().contains(expectedError.toLowerCase()));
passedAssertion = true;
}
if (hasErrorCodeNameField(expectedResult)) {
String expectedErrorCodeName = getErrorCodeNameField(expectedResult);
if (e instanceof MongoCommandException) {
assertEquals(expectedErrorCodeName, ((MongoCommandException) e).getErrorCodeName());
passedAssertion = true;
} else if (e instanceof MongoWriteConcernException) {
assertEquals(expectedErrorCodeName, ((MongoWriteConcernException) e).getWriteConcernError().getCodeName());
passedAssertion = true;
}
}
return passedAssertion;
}
use of com.mongodb.MongoWriteConcernException in project mongo-java-driver by mongodb.
the class AbstractClientSideEncryptionTest method shouldPassAllOutcomes.
@Test
public void shouldPassAllOutcomes() {
for (BsonValue cur : definition.getArray("operations")) {
BsonDocument operation = cur.asDocument();
String operationName = operation.getString("name").getValue();
BsonValue expectedResult = operation.get("result");
try {
BsonDocument actualOutcome = helper.getOperationResults(operation);
if (expectedResult != null) {
BsonValue actualResult = actualOutcome.get("result");
assertEquals("Expected operation result differs from actual", expectedResult, actualResult);
}
assertFalse(String.format("Expected error '%s' but none thrown for operation %s", getErrorContainsField(expectedResult), operationName), hasErrorContainsField(expectedResult));
assertFalse(String.format("Expected error code '%s' but none thrown for operation %s", getErrorCodeNameField(expectedResult), operationName), hasErrorCodeNameField(expectedResult));
} catch (RuntimeException e) {
boolean passedAssertion = false;
if (hasErrorContainsField(expectedResult)) {
String expectedError = getErrorContainsField(expectedResult);
assertTrue(String.format("Expected '%s' but got '%s' for operation %s", expectedError, e.getMessage(), operationName), e.getMessage().toLowerCase().contains(expectedError.toLowerCase()));
passedAssertion = true;
}
if (hasErrorCodeNameField(expectedResult)) {
String expectedErrorCodeName = getErrorCodeNameField(expectedResult);
if (e instanceof MongoCommandException) {
assertEquals(expectedErrorCodeName, ((MongoCommandException) e).getErrorCodeName());
passedAssertion = true;
} else if (e instanceof MongoWriteConcernException) {
assertEquals(expectedErrorCodeName, ((MongoWriteConcernException) e).getWriteConcernError().getCodeName());
passedAssertion = true;
}
}
if (!passedAssertion) {
throw e;
}
}
}
if (definition.containsKey("expectations")) {
List<CommandEvent> expectedEvents = getExpectedEvents(definition.getArray("expectations"), "default", null);
List<CommandEvent> events = commandListener.getCommandStartedEvents();
assertEventsEquality(expectedEvents, events);
}
BsonDocument expectedOutcome = definition.getDocument("outcome", new BsonDocument());
if (expectedOutcome.containsKey("collection")) {
List<BsonDocument> collectionData = collectionHelper.find();
List<BsonValue> expectedData = expectedOutcome.getDocument("collection").getArray("data").getValues();
assertEquals(collectionData.size(), expectedData.size());
int count = collectionData.size();
for (int i = 0; i < count; i++) {
BsonDocument actual = collectionData.get(i);
BsonDocument expected = expectedData.get(i).asDocument();
replaceTypeAssertionWithActual(expected, actual);
assertEquals(expected, actual);
}
}
}
Aggregations