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

722
Views
¿Cómo obtener el error de captura 404 en Axios?

Tengo este fragmento de código (parte de una función), observe el " BadURL " al final de la URL:

importar axios desde "axios";

 try { return axios.post("http://localhost:5000/api/featureFlagBadURL", { flagName: "newJqueryAjaxListener", defaultValue: "false", }); } catch (error) { return { data: 'false' } }

Pero no puede entrar en el bloque catch , dice:

 (node:7676) UnhandledPromiseRejectionWarning: Error: Request failed with status code 404

Puedo detectar el error solo si envuelvo la llamada de función fuera de la clase

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

0

Axios.post(...) es una llamada asíncrona que devuelve una promesa, esa declaración no falla, e incluso si lo hace, no se debe a que falle la solicitud HTTP.

Lo que necesita usar son los .then() y .catch() de la promesa devuelta para manejar la solicitud.

 return axios.post("http://localhost:5000/api/featureFlagBadURL", { flagName: "newJqueryAjaxListener", defaultValue: "false" }).then((results) => { console.log('yay', results); }).catch((error) => { console.log('oops', error); });

Otra alternativa es usar async await .

 async function handler() { try { const results = await axios.post("http://localhost:5000/api/featureFlagBadURL", { flagName: "newJqueryAjaxListener", defaultValue: "false", }); console.log('yay', results); } catch (error) { console.log('oops', error); return { data: 'false' }; } })
about 4 years ago · Juan Pablo Isaza Report

0

Prueba de esta manera:

 return axios.post("http://localhost:5000/api/featureFlagBadURL", { flagName: "newJqueryAjaxListener", defaultValue: "false", }).then(function (response) { // handle success console.log(response); }).catch(function (error) { // handle error console.log(error); }).then(function () { // always executed });

fuente

about 4 years ago · Juan Pablo Isaza Report

0

Puede detectar el error y verificar el código de estado. entonces puedes manejarlo. Tal vez cambie el caso de bruja o simplemente si. Lo hago por 404. ver el bacalao a continuación:

 axios.post("http://localhost:5000/api/featureFlagBadURL", { flagName: "newJqueryAjaxListener", defaultValue: "false", }).then((res) => { // handle response => res.data console.log(response); }).catch((error) => { if (error.response) { if(error.response.status === 404) { console.log('BAD URL') // or console.log(error.response.statusText) // "Not Found" } } }
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!