使用承诺 – 在失败处理程序中logging堆栈跟踪

我对nodejs比较陌生,所以我会更详细地解释一下我正在做的事情。

我有一个networking服务器。 如果请求失败,我想logging该exception的堆栈跟踪,但是传递错误页面而不会使服务器崩溃。

例如,处理请求的函数:

var Q = require('q'); var requestHandler = function () { // Here I get the data etc. that was requested. As this is not important, just a dummy here Q.resolve() // Now I answer the request .then(function (data) { // Dummy Code representing the sending of a response console.log('sending response …'); console.log('oh no! an exception'); // Now an Exception occurs throw new Error('You did something wrong!'); }) // If there was any error, show the error page instead .fail(function (err) { // Dummy Code representing the sending of the error page console.log('sending error page'); // Additionally I want to write the error into a log file console.error('You had an error: ', err); }) // If there was an error in the .fail-handler, I deserve the server to crash .done(); }; // A request comes in, I want to call my handler requestHandler(); 

控制台的输出:

 sending response … oh no! an exception sending error page You had an error: [Error: You did something wrong!] 

我看不到一种方法来访问堆栈跟踪。 但是,当我在.fail处理程序(或只是省略完整的.fail处理程序)抛出一个exception,我得到一个堆栈跟踪控制台(但我不得不重新启动服务器)。

所以我想我的问题是:

如何访问承诺失败处理程序中的堆栈跟踪?

编辑 :如何改善解释的任何提示,当然是受欢迎的。 如果我没有说清楚,请告诉我。

logging错误对象将不会打印错误的堆栈跟踪。 你需要特别要求:

 console.error('You had an error: ', err.stack);