Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Unhandled error in http.request, even with provided error event listener: getaddrinfo ENOTFOUND #8819

Closed
jednano opened this issue Dec 3, 2014 · 7 comments

Comments

@jednano
Copy link

jednano commented Dec 3, 2014

I'm calling http.request() and attaching an error event listener like so:

var http = require('http');
var req = http.request('http://foo.bar.baz/qux');
req.on('error', function(err) {
  console.log(err.message);
});

And I get the following error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: getaddrinfo ENOTFOUND
    at errnoException (dns.js:37:11)
    at Object.onanswer [as oncomplete] (dns.js:124:16)

Wrapping the request in a try...catch block still throws an unhandled error. My only work-around is to chain the event listener, like so:

var http = require('http');
http.request('http://foo.bar.baz/qux').on('error', function(err) {
  console.log(err.message);
}).end();

Which handles the error gracefully and as expected:

> getaddrinfo ENOTFOUND

I'm not quite sure how this should make any difference, but it does.

This should work without chaining, no?

Similar issues: #4846 #7729 #8647

@cjihrig
Copy link

cjihrig commented Dec 4, 2014

@jedmao what version of node are you using? This isn't reproducing for me on 0.11.14.

@jednano
Copy link
Author

jednano commented Dec 4, 2014

@cjihrig the current stable version v0.10.33

@micnic
Copy link

micnic commented Dec 4, 2014

@jedmao, tested on v0.10.32 and v0.10.33, works as expected, what OS are you using? looks like an synchronous request

@a0viedo
Copy link
Member

a0viedo commented Dec 5, 2014

@jedmao actually the chaining should resolve to exactly the same code order execution.

Tested on v0.8 and v0.11 branches and work as expected too. Don't ask me why but I immediately think on this. I don't trust semicolons anymore.

@jednano
Copy link
Author

jednano commented Dec 5, 2014

Try running just the first 2 lines of code and see the error.

var http = require('http');
var req = http.request('http://foo.bar.baz/qux');

Or type those 2 lines in a Node shell and see what I'm talking about. You shouldn't get an error here, just because you haven't yet attached an error handler, right? It should wait for req.end() to do whatever is throwing the error.

Tell me if this is somehow by design.

@cjihrig
Copy link

cjihrig commented Dec 5, 2014

I would say this is expected behavior, but could be clearer in the documentation. The http.request() call triggers the DNS lookup, which is giving you the error. You do have some time to add the error handler, but not using the REPL. I've verified that you can successfully add the error handler in the next tick:

var http = require('http');
var req = http.request('http://foo.bar.baz/qux');

process.nextTick(function() {
  req.on('error', function(err) {
    console.error(err.message);
  });
});

I still have not been able to reproduce your original issue (not to say that it isn't possible). And, as @a0viedo pointed out, the two code samples translate into the same thing.

@jednano
Copy link
Author

jednano commented Dec 6, 2014

Closing as by design.

@jednano jednano closed this as completed Dec 6, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants