[JAVA-267] update DBCusor .next() .hasNext() javadoc to say it can throw an exception WAS: MongoInternalException from DBCursor.hasNext() is inappropriate Created: 07/Feb/11  Updated: 17/Mar/11  Resolved: 07/Feb/11

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 2.4
Fix Version/s: 2.5

Type: Bug Priority: Minor - P4
Reporter: Andy Jefferson Assignee: Antoine Girbal
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

If I do the following

DBCursor curs = db.getCollection("Person").find(query);
while (curs.hasNext())
{
...
}

where query is "{ "firstName" : { "$eq" : "Daffy"}}

then I get
com.mongodb.MongoInternalException: couldn't get next element
at com.mongodb.DBCursor.hasNext(DBCursor.java:459)

Yes, I (now) know the query object represents invalid BSON (but then someone who is learning this API wouldn't know that, since "$eq" seems logical when you have "$ne", "$gt" etc).

The fact remains though that there is nothing in the API for Cursor.hasNext() (or java.util.Iterator.hasNext() too which is cleaimed to be implemented here) that permits such an exception. Invalid BSON should be detected in "DBCollection.find()" IMHO, so the user doesn't get unexpected exceptions thrown



 Comments   
Comment by Antoine Girbal [ 07/Feb/11 ]

changed:

  • these methods may now throw MongoException (removed MongoInternalException wrapper) which is the actual error that happened
  • change doc to state that exceptions may be thrown
Comment by auto [ 07/Feb/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: JAVA-267: update DBCursor .next() .hasNext() javadoc to say it can throw an exception WAS: MongoInternalException from DBCursor.hasNext() is inappropriate
https://github.com/mongodb/mongo-java-driver/commit/91dcab11ce7c152bc32523c3496fb838479235f2

Comment by Scott Hernandez (Inactive) [ 07/Feb/11 ]

If you use the Iterable/Iterator interface there is no place to document this. Users wouldn't even know where to look in the javadocs, most likley.

It is the same issue with checked exceptions since your can't alter the interface.

Comment by Eliot Horowitz (Inactive) [ 07/Feb/11 ]

We should update javadoc

Comment by Andy Jefferson [ 07/Feb/11 ]

So is the javadoc going to be updated ? since "works as designed" implies that they declare exactly what can happen, and those on this page
http://api.mongodb.org/java/2.5-pre-/index.html
certainly don't give a clue.

Comment by Eliot Horowitz (Inactive) [ 07/Feb/11 ]

find() doesn't actually go to the database.
since you can chain methods
.find().sort().limit().next()

It would be impossible to have find() throw all possible errors.

Comment by Andy Jefferson [ 07/Feb/11 ]

For the record, the log does detect this error, as
assertion 10068 invalid operator: $eq ns:nucleus.Person query:
{ firstName:

{ $eq: "Daffy" }

}

just that the Java API needs to detect it in a different place, via find().

Generated at Thu Feb 08 08:51:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.