2012-04-04 29 views
4

在我的應用程序的頂部,我有這樣的代碼:Node.js - 比process.on更好的錯誤處理('uncaughtException',fn)?

process.on('uncaughtException', function(err) { 
    console.log("We found an uncaught exception."); 
    console.log(err); 
}); 

如果我搜索我的日誌grep -i -A10 -B3 'uncaught exception' logfile,我可以得到一些不錯的信息。但我不知道異常拋出的執行上下文是什麼,錯誤是什麼等等。

應該使用什麼樣的模式?

回答

3

uncaughtException的「err」參數是一個對象,您可以請求其某些屬性以獲取有關該錯誤的更多信息。 正如你可以使用它的「堆棧」屬性有什麼地方出錯是來自更好地瞭解一個例子:

console.log(err.stack); 

參見:http://docs.nodejitsu.com/articles/errors/what-is-the-error-object

+0

謝謝約翰。 – 2012-04-04 17:22:51

相關問題