marcello3d / node-mongolian

[project inactive] Mongolian DeadBeef is an awesome Mongo DB driver for node.js
https://groups.google.com/group/node-mongolian
zlib License
350 stars 50 forks source link

Handling errors from within callbacks #93

Closed qharlie closed 12 years ago

qharlie commented 12 years ago

When the below executes, I never get to see 'HERE!', it just silently fails and doesn't give me any errors. How can I get runtime errors from within my callbacks ?

var server = new Mongolian({ host: 'mongodb-server', port: 27017}); var db = server.db('core'); var collection = db.collection('templates'); collection.find({}).skip(0).sort({'_id': 1}).limit(10).toArray(function (err, value) {

if (err) {
    logger.debug("Dao error = ", err);

}
else {
    logger.debug(value);
    someFuncThatDoesntExists();
    logger.debug("HERE!");
}

});

marcello3d commented 12 years ago

If you have no control over your code throwing an exception you'll need to add a try/catch block. If you're merely debugging, you'll want to consider using the uncaughtException feature of node.js: http://nodejs.org/api/process.html#process_event_uncaughtexception

That way you can at least print out the exception when it happens.

qharlie commented 12 years ago

But why are they getting swallowed? Exceptions everywhere else in my node code prints out a stack trace and an error ?

xcoderzach commented 12 years ago

+1 it causes assertion failures to silently get swallowed when testing :-(

qharlie commented 12 years ago

These are actually getting swallowed by mongodb, in line 310 of connection.js , it then emits a parseError if it finds an exception in the callback.

Maybe mongolian listens for a parseError and rethrows the error ?

qharlie commented 12 years ago

In case you didn't get my email, adding

connection.on('parseError', function(error)
    {
        throw error.trace;
    });

To MongolianServer() in server.js fixes the problem for me.

marcello3d commented 12 years ago

I'm not at my computer but it sounds like you're using an old version of Mongolian. The latest doesn't depend on mongodb and doesn't have that limitation (that annoyance was one of the many reasons I decided to remove the dependency).

Marcello

On Mar 12, 2012, at 7:03 PM, charles sandersreply@reply.github.com wrote:

In case you didn't get my email, adding

connection.on('parseError', function(error) { throw error.trace; });

To MongolianServer() in server.js fixes the problem for me.


Reply to this email directly or view it on GitHub: https://github.com/marcello3d/node-mongolian/issues/93#issuecomment-4465513

marcello3d commented 12 years ago

I can't seem to reproduce this with the latest mongolian.

Here's my slightly modified test code:

var Mongolian = require('mongolian')
var server = new Mongolian
var db = server.db('mongolian_test')
var collection = db.collection('templates')
collection.find({}).skip(0).sort({'_id': 1}).limit(10).toArray(function (err, value) {
    if (err) {
        console.log("Dao error = ", err);
    }
    else {
        console.log("value="+value);
        someFuncThatDoesntExists();
        console.log("HERE!");
    }
})

And its output (minus the mongolian debug logging—note that I don't actually have anything in the collection):

value=

~/workspace/test/test.js:11
        someFuncThatDoesntExists();
        ^
ReferenceError: someFuncThatDoesntExists is not defined
    at ~/workspace/test/test.js:11:9
    at ~/workspace/test/node_modules/mongolian/lib/cursor.js:208:13
    at ~/workspace/test/node_modules/mongolian/lib/util.js:19:13
    at [object Object].nextBatch (~/workspace/test/node_modules/mongolian/lib/cursor.js:153:9)
    at ~/workspace/test/node_modules/mongolian/lib/cursor.js:205:18
    at ~/workspace/test/node_modules/mongolian/lib/util.js:19:13
    at ~/workspace/test/node_modules/mongolian/lib/cursor.js:125:9
    at ~/workspace/test/node_modules/mongolian/lib/util.js:19:13
    at Connection.<anonymous> (~/workspace/test/node_modules/mongolian/lib/server.js:233:17)
    at Connection.emit (events.js:67:17)
marcello3d commented 12 years ago

Closing due because I can't reproduce. I can reopen it if you still have trouble.