Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
Catch a JavaScript error from a upper function

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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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)
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

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:

enter image description here

about 4 years ago · Juan Pablo Isaza Report

0

Maybe it's because you missed the error param after catch?

try {
  doSomeThing();
} catch (error) {
  console.error(error);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!