Error handling in SalatDAO

34 views
Skip to first unread message

randhindi

unread,
May 8, 2012, 9:18:00 PM5/8/12
to scala...@googlegroups.com
Hi Rose

Looking at the SalatDAO source, I noticed that you only check for an error by testing error.ok(). But Mongo will sometimes return ok() even though an error occurred.

For example, when trying to insert a document with an _id that already exists, the WriteResult.getLastError.ok() == true, but a new key "err" is added to the writeResult with the description of the error. 

Just in case this is something you might find useful, I use the following to test for an error upon inserting:

val error = wr.getLastError
if (error != null && (!error.ok() || error.contains("err"))) {
    None
}
else {
    dbo.getAs[ObjectId]("_id")
}


Also, is there any reason why you used getLastCachedError instead of getLastError? The latter will get the cached error if it exists, but fetch the error if its not cached.

Cheers

Rand

rktoomey

unread,
May 8, 2012, 10:44:06 PM5/8/12
to scala-salat
On advice at the time from 10gen. The plethora of different ways to
check for an error is rather deplorable, actually.

randhindi

unread,
May 9, 2012, 9:26:25 AM5/9/12
to scala...@googlegroups.com
Indeed, the way mongo deals with errors is rather confusing, and not very well documented! 
Reply all
Reply to author
Forward
0 new messages