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