我是相当新的这样的NodeJS,我会更详细一点就是我试图做解释。
我有一个网络服务器。 如果请求失败,我想日志异常的堆栈跟踪,而是提供一个错误页面,而不是导致服务器崩溃。
作为一个例子,功能处理的请求:
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处理程序),我得到的控制台上的堆栈跟踪(但我不得不重新启动服务器)。
所以我想我的问题是:
如何访问一个承诺堆栈跟踪失败的处理程序?
编辑 :关于如何提高解释任何提示的欢迎,当然。 如果我没有讲清楚,请让我知道。