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);
}
});
CodePudding user response:
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.