Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在失败处理程序中使用promises-记录堆栈跟踪_Javascript_Node.js_Stack Trace_Promise_Q - Fatal编程技术网

Javascript 在失败处理程序中使用promises-记录堆栈跟踪

Javascript 在失败处理程序中使用promises-记录堆栈跟踪,javascript,node.js,stack-trace,promise,q,Javascript,Node.js,Stack Trace,Promise,Q,我对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

我对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处理程序)时,我会在控制台上得到一个堆栈跟踪(但是我必须重新启动服务器)

所以我想我的问题是:

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


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

记录错误对象不会打印错误的堆栈跟踪。 您需要具体要求:

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

您不能用javascript构建跟踪并将其输出到客户端,而不是在服务器上查看它吗?我如何用javascript构建跟踪?我更喜欢将错误写入日志文件,而不是将其显示给网站访问者。谢谢Mariusz。为什么这不在q文档中?是不是在医生里,我只是错过了?他们给出的例子应该明确地说明这一点。所以它是纯JavaScript,与Q无关。在我的例子中,它肯定与Q有关。fail方法是Q的接口,对吗?如果失败,它会被Q调用,对吗?Q不想记录它发送的内容以及失败处理程序方法如何使用它吗?它似乎是文档中一个很明显的例子,更多的是关于“如何访问JavaScript错误对象上的堆栈跟踪”。Q.fail不会以任何方式改变error对象,它只是按照文档中的说明传递第一个参数。使用sourcemaps执行err.stack时,它显示的是
.js
文件,而不是源文件。是否有其他方法让浏览器写出源文件?