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

956
Visualizações
Prevent crash of application after Error: getaddrinfo ENOTFOUND at GetAddrInfoReqWrap.onlookup

I'm using Node.js 7.8.0 and I'm trying to send HTTP-request to some host, sometimes the desired host doesn't exist. In such case, I get the following message:

events.js:163
      throw er; // Unhandled 'error' event
      ^

Error: getaddrinfo ENOTFOUND js.js js.js:80
    at errnoException (dns.js:28:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:73:26)

Press any key to continue...

where js.js is the demo of non existent host. It's important to note, that everything works correctly if I send the same request to the existing host.

The problem is that this error leads to crash of my Node.js application, while I need to keep the app running.

I already checked, my HTTP-request doesn't contain http/https/port in the field of host.

I tried to use try/catch, domains, and res.on('error', exitHandlerCB) approaches, nothing works. The only way that worked for me is process.on('uncaughtException', exitHandlerCB);, but this one leads to the stop of my Node.js app.

Is there any way to catch the error due to request to non existing host and to continue the application flow?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Is there any way to catch the error due to request to non existing host and to continue the application flow?

Yes, what you need is listening error event on request object. Example code would be:

let http = require('http');

let clientRequest = http.get({
  hostname: 'www.not-exist-i9io90o9i8.com',
  port: 80,
  path: '/'
}, function (res) {
  console.log(res);
});
clientRequest.on('error', function(){
  console.log('error handler');
});

//...further operation

According to Node.js http document:

If any error is encountered during the request (be that with DNS resolution, TCP level errors, or actual HTTP parse errors) an 'error' event is emitted on the returned request object. As with all 'error' events, if no listeners are registered the error will be thrown.

The error catched in your case is error with DNS resolution.

over 4 years ago · Santiago Trujillo 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