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

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.