我嘗試了很多方法來嘗試從包中捕獲這個特定的 async/await 錯誤。我似乎無法捕捉到特定的錯誤。對于其他錯誤,它會捕獲它,但從這個包中,node.js 出于某種原因沒有捕獲它。(對不起,如果代碼格式不正確或有愚蠢的錯誤,我只是在這里輸入。)
我試過error, error.message, error.code, error.stack, error.toString(), 和error.message.toString().
以上所有內容都無法解決我的錯誤。
使用 Try Catch
async function main(){
try {
await testExample();
console.log('Finished!');
} catch (error){
if(error == 'specific error) {
throw new Error('an error has occured');
} else {
console.error(error);
}}}
main();
將 Try Catch 與附加到函式的 .catch 一起使用
async function main(){
try {
await testExample().catch(function (error) {
if(error == 'specific error) {
throw new Error('an error has occured');
}
});
console.log('Finished!');
} catch (error){
if(error == 'specific error) {
throw new Error('an error has occured');
} else {
console.error(error);
}
}}
main();
將 Try Catch 與 .catch 一起使用,附加到 main
async function main(){
try {
await testExample();
console.log('Finished!');
} catch (error){
if(error == 'specific error) {
throw new Error('an error has occured');
} else {
console.error(error);
}
}}
main().catch(function (error) {
if(error == 'specific error) {
throw new Error('an error has occured');
}
});;
使用 await-to-js
const to = promise => promise.then(res => [null, res]).catch(error=> [error|| true, null]);
async function main()
{
var [error, success] = await to(testExample());
if(error){
if(error == 'specific error) {
throw new Error('an error has occured');
} else {
console.error(error);
}
}
if(success){
console.log('Finished!');
main();
});
做出承諾
async function main(){
await new Promise((resolve, reject => {
testExample();
}).catch(function (error) {
if(error == 'specific error) {
throw new Error('an error has occured');
} else {
console.error(error);
}
});
console.log('Finished!');
}
main();
未捕獲的例外
process.on('uncaughtException', function(error) {
if(error == 'specific error) {
throw new Error('an error has occured');
} else {
console.error(error);
}
});
uj5u.com熱心網友回復:
您的問題的根源可以在這里找到:https ://github.com/fawazahmed0/youtube-uploader/blob/c2a33b0b680db2086be6d4b90a91b0157dca70eb/src/upload.ts#L658-L663
您要捕獲的錯誤被拋出changeHomePageLangIfNeeded,但鏈接的代碼顯示錯誤已被捕獲并記錄,但它不會上游傳遞給您的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/498347.html
