Search in sources :

Example 1 with MongoCommandException

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

the class CommandProtocol method sendFailedEvent.

private void sendFailedEvent(final ConnectionDescription connectionDescription, final long startTimeNanos, final CommandMessage commandMessage, final Throwable t) {
    if (commandListener != null) {
        Throwable commandEventException = t;
        if (t instanceof MongoCommandException && (SECURITY_SENSITIVE_COMMANDS.contains(getCommandName()))) {
            commandEventException = new MongoCommandException(new BsonDocument(), connectionDescription.getServerAddress());
        }
        sendCommandFailedEvent(commandMessage, getCommandName(), connectionDescription, startTimeNanos, commandEventException, commandListener);
    }
}
Also used : MongoCommandException(com.mongodb.MongoCommandException) BsonDocument(org.bson.BsonDocument)

Example 2 with MongoCommandException

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

the class Fixture method initializeCollection.

public static MongoCollection<Document> initializeCollection(final MongoNamespace namespace) {
    MongoDatabase database = getMongoClient().getDatabase(namespace.getDatabaseName());
    try {
        FutureResultCallback<Document> futureResultCallback = new FutureResultCallback<Document>();
        database.runCommand(new Document("drop", namespace.getCollectionName()), futureResultCallback);
        futureResultCallback.get(60, SECONDS);
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().startsWith("ns not found")) {
            throw e;
        }
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
    return database.getCollection(namespace.getCollectionName());
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) MongoCommandException(com.mongodb.MongoCommandException) Document(org.bson.Document)

Example 3 with MongoCommandException

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

the class Fixture method dropDatabase.

public static void dropDatabase(final String name) {
    if (name == null) {
        return;
    }
    try {
        FutureResultCallback<Document> futureResultCallback = new FutureResultCallback<Document>();
        getMongoClient().getDatabase(name).runCommand(new Document("dropDatabase", 1), futureResultCallback);
        futureResultCallback.get(60, SECONDS);
    } catch (MongoCommandException e) {
        if (!e.getErrorMessage().startsWith("ns not found")) {
            throw e;
        }
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}
Also used : FutureResultCallback(com.mongodb.async.FutureResultCallback) MongoCommandException(com.mongodb.MongoCommandException) Document(org.bson.Document)

Example 4 with MongoCommandException

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

the class FindOperation method exceptionTransformingCallback.

private static <T> SingleResultCallback<T> exceptionTransformingCallback(final SingleResultCallback<T> callback) {
    return new SingleResultCallback<T>() {

        @Override
        public void onResult(final T result, final Throwable t) {
            if (t != null) {
                if (t instanceof MongoCommandException) {
                    MongoCommandException commandException = (MongoCommandException) t;
                    callback.onResult(result, new MongoQueryException(commandException.getServerAddress(), commandException.getErrorCode(), commandException.getErrorMessage()));
                } else {
                    callback.onResult(result, t);
                }
            } else {
                callback.onResult(result, null);
            }
        }
    };
}
Also used : MongoCommandException(com.mongodb.MongoCommandException) SingleResultCallback(com.mongodb.async.SingleResultCallback) MongoQueryException(com.mongodb.MongoQueryException)

Example 5 with MongoCommandException

use of com.mongodb.MongoCommandException in project morphia by mongodb.

the class TestDocumentValidation method findAndModify.

@Test
public void findAndModify() {
    getMorphia().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    getDs().save(new DocumentValidation("Harold", 100, new Date()));
    Query<DocumentValidation> query = getDs().find(DocumentValidation.class);
    UpdateOperations<DocumentValidation> updates = getDs().createUpdateOperations(DocumentValidation.class).set("number", 5);
    FindAndModifyOptions options = new FindAndModifyOptions().bypassDocumentValidation(false);
    try {
        getDs().findAndModify(query, updates, options);
        fail("Document validation should have complained");
    } catch (MongoCommandException e) {
    // expected
    }
    options.bypassDocumentValidation(true);
    getDs().findAndModify(query, updates, options);
    Assert.assertNotNull(query.field("number").equal(5).get());
}
Also used : DocumentValidation(org.mongodb.morphia.entities.DocumentValidation) MongoCommandException(com.mongodb.MongoCommandException) Date(java.util.Date) Test(org.junit.Test)

Aggregations

MongoCommandException (com.mongodb.MongoCommandException)11 Document (org.bson.Document)4 FutureResultCallback (com.mongodb.async.FutureResultCallback)3 BsonDocument (org.bson.BsonDocument)3 Test (org.junit.Test)3 MongoSecurityException (com.mongodb.MongoSecurityException)2 MongoDatabase (com.mongodb.client.MongoDatabase)2 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)2 ValidationOptions (com.mongodb.client.model.ValidationOptions)2 Date (java.util.Date)2 Version (com.github.zafarkhaja.semver.Version)1 MongoClient (com.mongodb.MongoClient)1 MongoException (com.mongodb.MongoException)1 MongoQueryException (com.mongodb.MongoQueryException)1 SingleResultCallback (com.mongodb.async.SingleResultCallback)1 Book (org.mongodb.morphia.aggregation.AggregationTest.Book)1 CountResult (org.mongodb.morphia.aggregation.AggregationTest.CountResult)1 DocumentValidation (org.mongodb.morphia.entities.DocumentValidation)1