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

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

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 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