Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

389
Vistas
Handling server side errors in javascript fetch

I'm making a script which has server side validation. When validation fails I have the script throw an exception server side. I want to access the message in the exception when the response reaches the client side, but I'm having trouble with that. If I console.log the data I can see the exception, but I want to access it in the catch so I can push the error message to an HTML element on the page.

    fetch(/* Posting to some PHP script */).then(function(data) {
        
        // Logic here when success  
        console.log(data) // Shows error msg when there is an error 

        
    }).catch(function() {
        // Error Handling, I want to push the error msg to HTML element here             
    });

What am I doing wrong here? Thanks

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Throw the error. It will be caught in the .catch() and you can manipulate it there

fetch(/* Posting to some PHP script */)
    .then(function(data) {
        // Logic here when success 
        console.log(data) // Shows error msg when there is an error
        if(data.error){ // Or whatever condition you need to detect there's an error
            throw data.error; // This will be caught in the catch() below
        }
    })
    .catch(function(err) {
        // You have your error here with the message from the server, display it in the HTML          
    });
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda