Why the exception can't be caught !?

59 views
Skip to first unread message

Pedro Landeiro

unread,
Jun 18, 2011, 5:14:51 PM6/18/11
to nod...@googlegroups.com
Hello everyone,

Anyone knows why can't I caught the exception from a bad hostname like the example bellow:

    var http = require("http");
    var options = {host:"www.google.com..",path:"/",method:"GET"}; // notice the ".." at the end of the URL

    var request = http.request(options, function (res) {
        res.on('end', function (){
               console.log("end");
            });
    });

    request.on('error',function (err){
        console.log(err); // this should catch the error but does not!
    });

    request.end();


This ends with an exception "Error: EBADNAME, Misformatted domain name" but should be caught in the "on error" event, or not?

Thanks.

--
Pedro Landeiro
http://www.linkedin.com/in/pedrolandeiro

Marak Squires

unread,
Jun 18, 2011, 5:25:26 PM6/18/11
to nod...@googlegroups.com
Maybe you could try:


process.on('uncaughtException'function(err) {
  console.log(err);
});




--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

landeiro

unread,
Jun 18, 2011, 5:31:29 PM6/18/11
to nodejs
Hi Marak, thanks for your reply.

The "process.on", is a too "brute force" and I lose also the context
about the url that is misformatted.

On the deprecated "createClient" method, the handle of the "on error"
event works fine.


On Jun 18, 10:25 pm, Marak Squires <marak.squi...@gmail.com> wrote:
> Maybe you could try:
>
> process.on('uncaughtException', function(err) {
>   console.log(err);
>
> });
>
> http://debuggable.com/posts/node-js-dealing-with-uncaught-exceptions:...

Herman A. Junge

unread,
Jun 18, 2011, 5:59:31 PM6/18/11
to nodejs
Hi landeiro:

I'm getting this on a step by step (encapsulating the code into a
createServer() and then using the debugger into Chrome):

events.js:45
throw arguments[1]; // Unhandled 'error' event
^
Error: EBADNAME, Misformatted domain name
at dns.js:194:15
at Object.lookup (dns.js:190:11)
at Socket.connect (net.js:718:20)
at Object.createConnection (net.js:265:5)
at Agent._getConnection (http.js:1360:15)
at Agent._establishNewConnection (http.js:1178:21)
at Agent._cycle (http.js:1404:10)
at Agent.appendMessage (http.js:1160:8)
at Object._requestFromAgent (http.js:1436:27)
at Object.request (http.js:1449:18)

Checking the code around that line (194 in dns.js) , I've found that
the function channel.getDomain() in native code is the one which is
producing this exception.

Conclusion: Either you make sure yourself to input a well formatted
domain name, or you implement some measures to check the domain
entered.

hermanjunge



Ben Noordhuis

unread,
Jun 18, 2011, 6:04:27 PM6/18/11
to nod...@googlegroups.com
On Sat, Jun 18, 2011 at 23:31, landeiro <land...@gmail.com> wrote:
> Hi Marak, thanks for your reply.
>
> The "process.on", is a too "brute force" and I lose also the context
> about the url that is misformatted.
>
> On the deprecated "createClient" method, the handle of the "on error"
> event works fine.

You may have run into a bug: a quick glance at http.js and net.js
seems to suggest that errors emitted by the underlying net.Socket
aren't propagated to the http.ClientRequest object. That would explain
why your error handler never sees them.

landeiro

unread,
Jun 18, 2011, 6:12:18 PM6/18/11
to nodejs
Hi Ben,

That's my opinion too, the exception should be propagated to the
ClientRequest.
And is very strange because with the deprecated method works:

var http = require("http");

var client = http.createClient(80, 'www.google.com...');

client.on('error', function(err) {
console.log(err);
});

var request = client.request('HEAD', '/');

request.end();

On Jun 18, 11:04 pm, Ben Noordhuis <i...@bnoordhuis.nl> wrote:

Herman A. Junge

unread,
Jun 18, 2011, 6:13:36 PM6/18/11
to nodejs
Be advised that the error is produced before http.request is finished
forming, so I wouldn't attribute a bug in the event.

hermanjunge

On Jun 18, 6:04 pm, Ben Noordhuis <i...@bnoordhuis.nl> wrote:

landeiro

unread,
Jun 18, 2011, 6:17:15 PM6/18/11
to nodejs
I don't if it is a bug, but I think that I should be able to catch the
exception in some way, the problem is that I don't know how. :(

Ben Noordhuis

unread,
Jun 18, 2011, 6:25:08 PM6/18/11
to nod...@googlegroups.com
On Sun, Jun 19, 2011 at 00:13, Herman A. Junge <h...@neosource.cl> wrote:
> Be advised that the error is produced before http.request is finished
> forming, so I wouldn't attribute a bug in the event.

You can't catch the exception so that's no good. Test case:

https://gist.github.com/1033563

Herman A. Junge

unread,
Jun 18, 2011, 6:30:50 PM6/18/11
to nod...@googlegroups.com
Landeiro.

Use dns.resolve(), I did this test:

var dns = require('dns');

dns.resolve("www.google.com",'A', function(err, addresses) {

if(err) {
console.log(err);
}
else
{
console.dir(addresses);
}
});

// Returns [ '209.85.195.104' ]

var dns = require('dns');

dns.resolve("www.google.com..",'A', function(err, addresses) {

if(err) {
console.log(err);
}
else
{
console.dir(addresses);
}
});

/* returns 
* { stack: [Getter/Setter], * arguments: undefined, * type: undefined, * message: 'EBADNAME, Misformatted domain name', * errno: 8, * code: 'EBADNAME' }
*/

hermanjunge





landeiro

unread,
Jun 18, 2011, 6:43:15 PM6/18/11
to nodejs
Thanks Herman,

that works indeed, I allready tried, the "problem" with that approach
is that I have to query twice the DNS module to make the request.
1st to check if the domaind is right.
2nd when the ClientRequest uses it implicity.

But the question remains, why can I catch the exception when it
happens.

:(

Ben Noordhuis

unread,
Jun 18, 2011, 6:56:13 PM6/18/11
to nod...@googlegroups.com
On Sun, Jun 19, 2011 at 00:43, landeiro <land...@gmail.com> wrote:
> But the question remains, why can I catch the exception when it
> happens.

Because it's a bug. I've taken the liberty to file a bug report.

https://github.com/joyent/node/issues/1202

dhruvbird

unread,
Jun 19, 2011, 1:54:02 AM6/19/11
to nodejs
Hello,
This bug _may_ be related to this one - DNS lookup() callbacks are
called synchronously at times. https://github.com/joyent/node/issues/1164
Maybe they are called before the 'error' handler is attached to the
http request object - just a hunch, could be totally off.

Regards,
-Dhruv.

On Jun 19, 3:56 am, Ben Noordhuis <i...@bnoordhuis.nl> wrote:

landeiro

unread,
Jun 19, 2011, 9:25:17 AM6/19/11
to nodejs
It seems that your right, Dhruv.
Thanks.

On Jun 19, 6:54 am, dhruvbird <dhruvb...@gmail.com> wrote:
> Hello,
>   This bug _may_ be related to this one - DNS lookup() callbacks are
> called synchronously at times.https://github.com/joyent/node/issues/1164
Reply all
Reply to author
Forward
0 new messages