use of com.mongodb.MongoException in project mongo-java-driver by mongodb.
the class ProtocolHelper method throwWriteException.
@SuppressWarnings("deprecation")
private static void throwWriteException(final BsonDocument result, final ServerAddress serverAddress) {
MongoException specialException = createSpecialException(result, serverAddress, "err");
if (specialException != null) {
throw specialException;
}
int code = WriteConcernException.extractErrorCode(result);
if (ErrorCategory.fromErrorCode(code) == ErrorCategory.DUPLICATE_KEY) {
throw new DuplicateKeyException(result, serverAddress, createWriteResult(result));
} else {
throw new WriteConcernException(result, serverAddress, createWriteResult(result));
}
}
use of com.mongodb.MongoException in project mongo-java-driver by mongodb.
the class InternalStreamConnection method open.
@Override
public void open() {
isTrue("Open already called", stream == null);
stream = streamFactory.create(serverId.getAddress());
try {
stream.open();
description = connectionInitializer.initialize(this);
opened.set(true);
connectionListener.connectionOpened(new ConnectionOpenedEvent(getId()));
LOGGER.info(format("Opened connection [%s] to %s", getId(), serverId.getAddress()));
} catch (Throwable t) {
close();
if (t instanceof MongoException) {
throw (MongoException) t;
} else {
throw new MongoException(t.toString(), t);
}
}
}
use of com.mongodb.MongoException in project mongo-java-driver by mongodb.
the class AsyncQueryBatchCursor method handleGetMoreQueryResult.
private void handleGetMoreQueryResult(final AsyncConnection connection, final SingleResultCallback<List<T>> callback, final QueryResult<T> result) {
if (isClosed()) {
connection.release();
connectionSource.release();
callback.onResult(null, new MongoException("The cursor was closed before next() completed."));
return;
}
cursor.getAndSet(result.getCursor());
if (result.getResults().isEmpty() && result.getCursor() != null) {
getMore(connection, result.getCursor(), callback);
} else {
count += result.getResults().size();
if (limitReached()) {
killCursor(connection);
connection.release();
} else {
connection.release();
connectionSource.release();
}
if (result.getResults().isEmpty()) {
callback.onResult(null, null);
} else {
callback.onResult(result.getResults(), null);
}
}
}
use of com.mongodb.MongoException in project spring-boot by spring-projects.
the class MongoHealthIndicatorTests method mongoIsDown.
@Test
public void mongoIsDown() throws Exception {
MongoTemplate mongoTemplate = mock(MongoTemplate.class);
given(mongoTemplate.executeCommand("{ buildInfo: 1 }")).willThrow(new MongoException("Connection failed"));
MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate);
Health health = healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
assertThat((String) health.getDetails().get("error")).contains("Connection failed");
verify(mongoTemplate).executeCommand("{ buildInfo: 1 }");
}
use of com.mongodb.MongoException in project graylog2-server by Graylog2.
the class MongoConnectionImpl method connect.
/**
* Connect the instance.
*/
@Override
public synchronized Mongo connect() {
if (m == null) {
final String dbName = mongoClientURI.getDatabase();
if (isNullOrEmpty(dbName)) {
LOG.error("The MongoDB database name must not be null or empty (mongodb_uri was: {})", mongoClientURI);
throw new RuntimeException("MongoDB database name is missing.");
}
m = new MongoClient(mongoClientURI);
db = m.getDB(dbName);
db.setWriteConcern(WriteConcern.ACKNOWLEDGED);
mongoDatabase = m.getDatabase(dbName).withWriteConcern(WriteConcern.ACKNOWLEDGED);
}
try {
db.command("{ ping: 1 }");
} catch (MongoCommandException e) {
if (e.getCode() == 18) {
throw new MongoException("Couldn't connect to MongoDB. Please check the authentication credentials.", e);
} else {
throw new MongoException("Couldn't connect to MongoDB: " + e.getMessage(), e);
}
}
final Version mongoVersion = getMongoVersion(m.getDB("admin"));
if (mongoVersion != null && mongoVersion.lessThan(MINIMUM_MONGODB_VERSION)) {
LOG.warn("You're running MongoDB {} but Graylog requires at least MongoDB {}. Please upgrade.", mongoVersion, MINIMUM_MONGODB_VERSION);
}
return m;
}
Aggregations