2014-10-27 152 views
0

在我的項目我的例外我試圖捕捉例外,而使用mongoose.createConnection打開連接,對這些代碼得到一個錯誤時做什麼:如何處理未處理在node.js中

var createdDb = mongoose.createConnection(connectionString); 
createdDb.on("error", function (err) { 
    console.error("Error connecting to Mongo", err); 
}); 

但我的問題是,在一般 - 我怎麼捕捉任何未捕獲的異常,並顯示錯誤(作爲一個簡單的字符串)?我在app.js這個代碼,但它似乎沒有例外越來越有:

app.use(function (err, req, res, next) { 
    res.status(err.status || 500); 
    res.render('error', { 
     message: err.message, 
     error: {} 
    }); 
}); 

回答

0
process.on('uncaughtException', function(err) { 
    console.log('Caught exception: ' + err); 
});