Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda