Express,节点错误控制流程

时间:2013-08-20 14:16:26

标签: node.js express node-mongodb-native

检查下面的代码,try / catch块如何捕获“路径错误”?我的理解是注册到'/'路由的回调是在主事件循环上执行的,因此指定的try / catch块不能捕获异常,因为它不是执行回调的调用堆栈的一部分。

var express = require('express')   ;
var app = express();
var http = require('http');

var mongod = require('mongodb');

var server_conf = new mongod.Server('localhost', 27017, {auto_reconnect:true});

//dummy logger
var logger =  {
    error:function(message, object) {console.log('anything')},
    log:function(message, object) {console.log('anything')},
    debug:function(message, object) {console.log('anything')}}

var db_container = {db: new mongod.Db('test', server_conf,
    {w:1,
        journal:true, native_parser:true, logger: logger})}

app.use(express.bodyParser());
app.use(app.router);

db_container.db.open(function(err, index_info){

    if(err) throw err;
    var testcol = db_container.db.collection('testcol');
    try{
        app.get('/', function(request, res){
           throw new Error("Route Error");
            testcol.insert({hello:"moto"}, function(err,doc){

                if(err){
                    throw err;
                }
                testcol.find({}).toArray(function(err,docs){
                    res.send(docs);
                });
            });

        });
    }
    catch(e){
        res.send("err caught "+ e);
    }
    http.createServer(app).listen(3000, function () {
        console.log('Express server listening on port ' + '3000');
    });
});

相关问题是为什么以下“崩溃我”错误不会导致节点应用程序崩溃。抛出此错误的所有函数都在主事件循环上运行并且应该导致未捕获的异常,从而导致应用程序崩溃吗?除非本机mongo驱动程序正在捕获错误并悄悄地将其扼杀......

var express = require('express')   ;
var app = express();
var http = require('http');

var mongod = require('mongodb');

var server_conf = new mongod.Server('localhost', 27017, {auto_reconnect:true});

//dummy logger
var logger =  {
    error:function(message, object) {console.log('anything')},
    log:function(message, object) {console.log('anything')},
    debug:function(message, object) {console.log('anything')}}

var db_container = {db: new mongod.Db('test', server_conf,
    {w:1,
        journal:true, native_parser:true, logger: logger})}

app.use(express.bodyParser());
app.use(app.router);

db_container.db.open(function(err, index_info){

    if(err) throw err;
    var testcol = db_container.db.collection('testcol');
    try{
        app.get('/', function(request, res){

            testcol.insert({hello:"moto"}, function(err,doc){
                throw new Error("Crash Me");

                if(err){
                    throw err;
                }
                testcol.find({}).toArray(function(err,docs){
                    res.send(docs);
                });
            });

        });
    }
    catch(e){
        res.send("err caught "+ e);
    }
    http.createServer(app).listen(3000, function () {
        console.log('Express server listening on port ' + '3000');
    });
});

1 个答案:

答案 0 :(得分:0)

问题第一部分的答案是here

至于第二部分,问题仍然存在,但我认为这是由于某些尝试捕获节点-mongodb-native默默地扼杀抛出的异常。

您可以在node-mongodb-native组here上找到任何相关回复