一篇纯粹的异步错误处理整理文,以例子为基本进行解释。
node -v
8.4.0
function PromiseRejected() {
return Promise.reject(new Error("Error in PromiseRejected"));
}
function PromiseError() {
return new Promise(() => {
throw new Error("Error in PromiseError");
});
}
async function PromiseLv1() {
return await PromiseRejected();
// return await PromiseError();
}
async function PromiseLv2() {
return await PromiseLv1();
}
async function main() {
await PromiseLv2();
}
main().then(_ => _);
注释的部分可以调换直接抛错和使用reject,结果都一样:
(node:25564) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Error in PromiseRejected
(node:25564) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
对上面的例子main部分进行修改:
async function main() {
try {
await PromiseLv2();
} catch (e) {
console.log(e);
}
}
错误会得到捕获,并打印出来:
Error: Error in PromiseRejected | PromiseError
at PromiseRejected (/Prog/Codes/NodeJs/SASDN/async/index.js:2:25)
at PromiseLv1 (/Prog/Codes/NodeJs/SASDN/async/index.js:10:16)
at PromiseLv2 (/Prog/Codes/NodeJs/SASDN/async/index.js:15:16)
at main (/Prog/Codes/NodeJs/SASDN/async/index.js:20:11)
at Object.<anonymous> (/Prog/Codes/NodeJs/SASDN/async/index.js:26:1)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
在无捕获的情况下可以下面的方法进行全局处理:
process.on('unhandledRejection', function(reason, p){
console.log("Possibly Unhandled Rejection at: Promise ", p, " reason: ", reason);
});
process.on('uncaughtException', (err) => {
fs.writeSync(1, `Caught exception: ${err}\n`);
});
function PromiseRejected() {
return Promise.reject(new Error("Error in PromiseRejected"));
}
function PromiseError() {
return new Promise(() => {
throw new Error("Error in PromiseError");
});
}
function PromiseLv1() {
let exec = PromiseRejected;
// let exec = PromiseError;
return exec();
}
function PromiseLv2() {
return PromiseLv1();
}
function main() {
PromiseLv2().then(_ => _);
}
main();
Promise版本也是一样的,未捕获的错误就会直接出错:
(node:45779) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Error in PromiseRejected
(node:45779) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
最后可以使用catch的方法捕获错误:
function main() {
PromiseLv2().then(_ => _).catch((e) => {
console.log(`Caught in main: ${e.stack}`);
});
}
EOF