Joh*_*ing 12 javascript node.js
假设我有12个async/await函数,并且在12日深处,会发生错误.现在,我有这个代码来捕获所有错误:
process.on('unhandledRejection', function {
console.error(err);
process.exit(1);
});
Run Code Online (Sandbox Code Playgroud)
问题是,不返回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
Run Code Online (Sandbox Code Playgroud)
在其他项目中,当我使用具有以下结构的回调时:
function doSomething(err, done) {
if (err) { return done(err); }
/* do something */
return done(null, true);
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个很好的堆栈跟踪错误发生的位置和导致那里的步骤.现在,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));
Run Code Online (Sandbox Code Playgroud)
unhandledRejection错误处理程序可能不是尝试捕获所有这些的正确方法。我建议将 async/await 包装在 try/catch 块中:
async function doSomething() {
try {
await doSomethingElse()
} catch(err) {
console.log(err)
}
}
Run Code Online (Sandbox Code Playgroud)
这应该会给你一个更好的堆栈跟踪。
| 归档时间: |
|
| 查看次数: |
1180 次 |
| 最近记录: |