假设我有 12 个 async/await
函数,在第 12 个深度,发生错误。现在,我有这段代码可以捕获所有错误:
process.on('unhandledRejection', function {
console.error(err);
process.exit(1);
});
问题是,stacktrace 没有返回:
ReferenceError: sdfg is not defined
- get.js:29 Fruit.module.exports [as get]
/project/models/fruit/get.js:29:2
- next_tick.js:129 process._tickDomainCallback
internal/process/next_tick.js:129:7
在其他项目中,当我使用以下结构的回调时:
function doSomething(err, done) {
if (err) { return done(err); }
/* do something */
return done(null, true);
}
然后,我对错误发生的位置以及导致错误的步骤进行了很好的堆栈跟踪。现在使用 async/await
我已经尝试在各种级别捕获错误但没有结果。我也尝试过 longjohn
和 stackup
- 我仍然只得到最后一个引发错误的函数。
帮助 - 我如何查看完整的堆栈?!捕获嵌套的 async/await 错误的正确方法是什么?
编辑:(一个完整的例子)
const getA = async () => {
await getB();
}
const getB = async () => {
await getC();
sdgf();
}
const getC = async () => {}
const start = async () => {
await getA();
}
start().then().catch(e => console.error(e));
最佳答案
此问题似乎已在 Node 12 中得到解决。使用 v12.6.0
,原始帖子中的片段会产生以下堆栈跟踪:
ReferenceError: sdgf is not defined
at getB (/path/to/test.js:7:3)
at async getA (/path/to/test.js:2:3)
at async start (/path/to/test.js:15:3)
关于javascript - 使用 async/await 时如何获取完整的 Node.js 堆栈跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43328672/