Skip to content

Commit b1a588d

Browse files
committed
[JAVA-276]: findAndModify does not throw MongoException on error, fails silently
1 parent 6a50a6f commit b1a588d

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,10 @@ public DBObject findAndModify(DBObject query, DBObject fields, DBObject sort, bo
319319

320320
if (remove && !(update == null || update.keySet().isEmpty() || returnNew))
321321
throw new MongoException("FindAndModify: Remove cannot be mixed with the Update, or returnNew params!");
322-
323-
return (DBObject) this._db.command( cmd ).get( "value" );
322+
323+
CommandResult res = this._db.command( cmd );
324+
res.throwOnError();
325+
return (DBObject) res.get( "value" );
324326
}
325327

326328

src/test/com/mongodb/JavaClientTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,13 @@ public void testFindAndModify(){
681681
assertEquals( 2 , dbObj.keySet().size());
682682
assertEquals( 5 , dbObj.get( "x" ));
683683
assertNull( c.findOne(new BasicDBObject( "_id" , 1 ) ));
684-
684+
685+
// test exception throwing
686+
try {
687+
dbObj = c.findAndModify( null, null );
688+
assertTrue(false, "Exception not throw when no update nor remove");
689+
} catch (MongoException e) {
690+
}
685691
}
686692

687693
@Test

0 commit comments

Comments
 (0)