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

955
Views
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 answers
Answer question

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