7
假设我有12 async/await
函数,并且在第12个函数中,发生错误。现在,我有这样的代码来捕获所有的错误:如何在使用async/await时获取完整的Node.js堆栈跟踪?
process.on('unhandledRejection', function {
console.error(err);
process.exit(1);
});
的问题是,没有返回堆栈跟踪:
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
- 我仍然只得到抛出错误的最后一个函数。
帮助 - 如何查看完整的堆栈?捕捉嵌套异步/等待错误的正确方法是什么?
编辑:(一个完整的例子)
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));
这是什么,得到: \t的ReferenceError:sdfg不是在Fruit.module.exports [如GET]定义 \t(/models/fruit/get.js:29:2) \t在process._tickDomainCallback (internal/process/next_tick.js:129:7) –
@JohnDerring看起来像一个堆栈跟踪,说sdfg没有在果实模型的第29行定义。我错过了什么? –
获得此功能之前的其他11个功能?换句话说,完整的堆栈跟踪...? –