如何从软件包中捕获特定的异步/等待错误?

发布于 2025-02-10 09:15:14 字数 2326 浏览 2 评论 0原文

我尝试了许多方法来尝试从软件包中捕获这种特定的异步/等待错误。我似乎无法遇到特定的错误。对于其他错误,它会捕获它,但是从此软件包中,Node.js不会出于某种原因捕获它。 (对不起,如果代码未正确格式化或有愚蠢的错误,我只是在这里键入它。)

我尝试了错误error.message.messageerror。代码error.stackerror.tostring()error.message.tostring()

以上所有内容都无法捕获我的错误。

使用尝试捕获

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和.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();

使用catch和.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().catch(function (error) {
if(error == 'specific error) {
throw new Error('an error has occured');
}
});;

。 -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();

unduffing例外

process.on('uncaughtException', function(error) {
   if(error == 'specific error) {
throw new Error('an error has occured');
} else {
console.error(error);
}
});

I have tried many ways to try and catch this specific async/await error from a package. I can't seem to catch a specific error. For other errors, it catches it but from this package, node.js does not catch it for some reason. (Sorry if the code is not formatted correctly or has silly mistakes I'm just typing it here.)

I have tried error , error.message, error.code, error.stack, error.toString(), and error.message.toString().

All of the above do not work to catch my error.

Using 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();

Using Try Catch Along with .catch attached to function

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();

Using Try Catch Along with .catch, attached to 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');
}
});;

Using 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();   
});

Making a promise

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();

Uncaught Exception

process.on('uncaughtException', function(error) {
   if(error == 'specific error) {
throw new Error('an error has occured');
} else {
console.error(error);
}
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩起发的微风 2025-02-17 09:15:14

The source of your problem can be found here: https://github.com/fawazahmed0/youtube-uploader/blob/c2a33b0b680db2086be6d4b90a91b0157dca70eb/src/upload.ts#L658-L663

The error you want to catch is thrown in changeHomePageLangIfNeeded, but链接的代码显示该错误已被捕获和记录,但并未传递到您的代码上游。

The source of your problem can be found here: https://github.com/fawazahmed0/youtube-uploader/blob/c2a33b0b680db2086be6d4b90a91b0157dca70eb/src/upload.ts#L658-L663

The error you want to catch is thrown in changeHomePageLangIfNeeded, but the linked code shows the error is caught and logged, but it isn't passed upstream to your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文