This is just example code of my code
async function thisThrows() {
throw new Error("Thrown from thisThrows()");
}
async function run() {
try {
await thisThrows();
} catch (e) {
throw new Error(e)
}
}
async function run1() {
try{
await run()
}catch(e){
throw new Error(e);
}
}
run1().catch(error => {
console.log(error);
});
following code snippet is giving me nested error output i.e Error: Error: Error
Error: Error: Error: Thrown from thisThrows()
at run1 (/Users/saunish/servify/sandbox/error-handling.js:18:15)
i need output to be
Error: Thrown from thisThrows()
CodePudding user response:
This is because you are catching the errors and then creating new ones and throwing the new ones instead of the original error.
All functions should actually rethrow the original error, for example:
async function run() {
try {
await thisThrows();
} catch (e) {
throw e // just rethrow the original
}
}