Search in sources :

Example 1 with MongoWriteConcernException

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));
}
Also used : ServerAddress(com.mongodb.ServerAddress) CommandWriteTransformer(com.mongodb.internal.operation.CommandOperationHelper.CommandWriteTransformer) WriteConcernHelper.hasWriteConcernError(com.mongodb.internal.operation.WriteConcernHelper.hasWriteConcernError) BsonBoolean(org.bson.BsonBoolean) WriteConcernHelper.createWriteConcernError(com.mongodb.internal.operation.WriteConcernHelper.createWriteConcernError) BsonDocument(org.bson.BsonDocument) MongoWriteConcernException(com.mongodb.MongoWriteConcernException) Connection(com.mongodb.internal.connection.Connection) BsonArray(org.bson.BsonArray) WriteConcernResult(com.mongodb.WriteConcernResult) CommandWriteTransformerAsync(com.mongodb.internal.operation.CommandOperationHelper.CommandWriteTransformerAsync) BsonInt32(org.bson.BsonInt32) AsyncConnection(com.mongodb.internal.connection.AsyncConnection) BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) MongoWriteConcernException(com.mongodb.MongoWriteConcernException)

Example 2 with MongoWriteConcernException

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;
}
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 3 with MongoWriteConcernException

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;
    });
}
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 4 with MongoWriteConcernException

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;
}
Also used : MongoException(com.mongodb.MongoException) MongoCommandException(com.mongodb.MongoCommandException) MongoWriteConcernException(com.mongodb.MongoWriteConcernException) ClusterFixture.getConnectionString(com.mongodb.ClusterFixture.getConnectionString) ClusterFixture.getMultiMongosConnectionString(com.mongodb.ClusterFixture.getMultiMongosConnectionString) BsonString(org.bson.BsonString) ConnectionString(com.mongodb.ConnectionString)

Example 5 with MongoWriteConcernException

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);
        }
    }
}
Also used : BsonDocument(org.bson.BsonDocument) MongoCommandException(com.mongodb.MongoCommandException) CommandEvent(com.mongodb.event.CommandEvent) MongoWriteConcernException(com.mongodb.MongoWriteConcernException) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue) JsonTestServerVersionChecker.skipTest(com.mongodb.JsonTestServerVersionChecker.skipTest) Test(org.junit.Test)

Aggregations

MongoWriteConcernException (com.mongodb.MongoWriteConcernException)8 BsonDocument (org.bson.BsonDocument)6 BsonString (org.bson.BsonString)6 MongoException (com.mongodb.MongoException)4 MongoCommandException (com.mongodb.MongoCommandException)3 WriteConcernResult (com.mongodb.WriteConcernResult)3 BsonInt32 (org.bson.BsonInt32)3 Test (org.junit.Test)3 MongoWriteException (com.mongodb.MongoWriteException)2 ServerAddress (com.mongodb.ServerAddress)2 WriteConcernError (com.mongodb.bulk.WriteConcernError)2 BsonArray (org.bson.BsonArray)2 ClusterFixture.getConnectionString (com.mongodb.ClusterFixture.getConnectionString)1 ClusterFixture.getMultiMongosConnectionString (com.mongodb.ClusterFixture.getMultiMongosConnectionString)1 ConnectionString (com.mongodb.ConnectionString)1 JsonTestServerVersionChecker.skipTest (com.mongodb.JsonTestServerVersionChecker.skipTest)1 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1 WriteConcern (com.mongodb.WriteConcern)1 WriteError (com.mongodb.WriteError)1 ConnectionDescription (com.mongodb.connection.ConnectionDescription)1