I'm stuck with the following problem:
function upperFn(){
FetchSomeThing()
.catch(err => {
console.log(err)
})
}
function lowerFn(){
try {
upperFn()
}
catch (err){
//Here its not working anymore
console.log(err) // Catch is never used
}
}
So I've tried to return the error and even rethrow it but nothing work's. I would be very happy if someone can explain me how to catch this error in my lower function.
Thanks
I just figure out how to handle this probleme, simply by returning the whole function.
function upperFn(){
return FetchSomeThing()
}
function lowerFn(){
try {
upperFn()
}
catch (err){
//Here its working well
console.log(err)
}
}
Simply throw the error in upperFunction and try-catch it in the lowerFunction
function FetchSomeThing() {
return Promise.reject("Something goes wrong.");
}
function upperFn(){
FetchSomeThing().catch(error => {
throw error;
})
}
function lowerFn(){
try {
upperFn()
}
catch (error) {
//Here its not working anymore
console.log(error) // Catch is never used
}
}
lowerFn()
Working example: https://codesandbox.io/s/agitated-thunder-3s9qj
Output:
Maybe it's because you missed the error param after catch?
try {
doSomeThing();
} catch (error) {
console.error(error);
}