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

239
Visualizações
Why does Vue's errorHandler fail to catch axios's error?

I set up a global errorHandler in Vue3's main.js:

app.config.errorHandler = error => {
    console.log("catch error")
}

In general methods, it works well. For example:

methods: {
  showMessage() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        reject("async error!")
      }, 500)
    })
  }
}

It print catch error in console, But when I use Axios to make a request, it can't catch axios's Uncaught (in promise) Error. Such as:

methods: {
  showMessage() {
    this.$axios.get('/wrong-url/').then(response => {
      console.log('wrong url')
    })
  }
}

The global errorHandler cannot catch the error at this time. The console displays the error:

Uncaught (in promise) Error: Request failed with status code 404

I've googled it, but I can't find the reason.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The promise is not returned and isolated inside a method, which is a mistake. With a few exceptions, promise chain should always be preserved, this allows errors to be handled:

  showMessage() {
    return this.$axios.get('/wrong-url/').then(response => {
    ...

A fool-proof way is to use async..await because it forces a function to return a promise:

  async showMessage() {
    const response = await this.$axios.get('/wrong-url/');
    ...

It can be combined linter rule to avoid mistakes with promise chaining.

about 4 years ago · Juan Pablo Isaza Relatório

0

config.errorHandler is for handling frontend errors. Yоu still have to create an interceptor when the app is created:

...
created() {
    axios.interceptors.response.use((response) => {
        return response;
    }, function(error) {
        // Do something ...

        return Promise.reject(error);
    });
}
...
app.config.errorHandler = (err, instance, info) => {
    // Do something ...
};
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