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

199
Visualizações
How to raise error intentionally with express

After sending an axios request to my Node server, how can I respond where I "reject" the Axios route/request and end up in the "catch()" of my client side code? I know "res.send(data)" or "res.json(data)" will resolve into "then()" of the promise. Is there anything I can respond with that will reject it?

axios.post('/edit-item', {_id: id, updateText: editInput.value, author: author}).then((response) => {
    e.target.parentElement.parentElement.children[3].innerHTML = `${response.data}`
}).catch(() => {
    //I want to end up here by responding with something from the server
})
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can raise an error in Node.js which will be handled by the internal error handler, or you may return the status code of your choice

Raising an error:

app.post(req, res, next) {
  throw new Error('Something went wrong')

Returning error message:

app.post(req, res, next) {
  res.status(500).send({ error: 'Something failed!' })
}

instead of 500 you can enter any error status code there

Once you return an error code, axios will go into the catch section

You can find more about error handling here: error handling

about 4 years ago · Juan Pablo Isaza Relatório

0

Return anything with http status code not in range status >= 200 && status < 300 from your server will get you in the .catch block by default:

  // `validateStatus` defines whether to resolve or reject the promise for a given
  // HTTP response status code. If `validateStatus` returns `true` (or is set to `null`
  // or `undefined`), the promise will be resolved; otherwise, the promise will be
  // rejected.
  validateStatus: function (status) {
    return status >= 200 && status < 300; // default
  }

https://axios-http.com/docs/handling_errors

You can override validateStatus to define in which cases the axios Promise is resolved/rejected

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