Errors and callbacks in Node.js's node-mongodb-native

node-mongodb-native has the usual error handling mechanism of Node.js: we pass in a callback to handle results, and the first parameter is an error that will be null if no error occurred. For example:

Most such structures in Node seem to execute the callback outside of an error context. However, in node-mongodb-native, the toArray and nextObject callbacks (and by extension a couple of others) are wrapped in a try-catch that triggers the error callback. What does this mean? If your callback throws an exception, the same callback will be invoked with the error parameter set. Thus your callback may be invoked twice: once with successful data, and once with an error corresponding to the exception thrown within your own callback.

So if you're seeing some strange double-invocations of your callback, that's probably what's cracking.

UPDATE: See issue 81 on the node-mongodb-driver github repository to track the resolution of this issue.