title: How to catch getaddrinfo ENOTFOUND
body: <p>I have a list of links that I need to check before processing some data. Checking headers with http.get returns error: </p>
  
  <pre><code>events.js:72   
          throw er; // Unhandled 'error' event    
            ^    
  Error: getaddrinfo ENOTFOUND       
      at errnoException (dns.js:37:11) 
  </code></pre>
  
  <p>I cannot handle this error, and exits the process. I tried res.on("error") and try..catch on http.get but nothing works.</p>
  
  <p>Below is the code snippet, and <a href="http://runnable.com/Uve8wMXd3EcTAAA7/error-with-http-get-headers-for-node-js" rel="noreferrer">here is live example at runnable.com</a></p>
  
  <pre><code>//This is OK
  getHeaders('http://google.com/404pag-that-does-not-exit');
  
  
  //Here is the error.
  //Uncoughtable error!
  getHeaders('http://doesnotexistooooo.com');
  
  function getHeaders(link){
      var _http = require("http");
      var myUrl = require("url");
  
      var qs=(myUrl.parse(link).search==null) ? "" : myUrl.parse(link).search ;
      var path=myUrl.parse(link).pathname;
  
      var options = {
          hostname: myUrl.parse(link).hostname,
          path: path+qs,
          method: 'HEAD'
      };
      _http.get(options, function(res) {
          res.on('error',function(e){
              console.log("Error: " + myUrl.parse(link).hostname + "\n" + e.message); 
              console.log( e.stack );
          });
          console.log('STATUS: ' + res.statusCode);
          console.log('HEADERS: ' + JSON.stringify(res.headers));
      });
  
  }
  </code></pre>