Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:38:37+00:00 2026-06-02T06:38:37+00:00

using node http package it doesn’t seem possible to catch the exception caused by

  • 0

using node http package it doesn’t seem possible to catch the exception caused by opening a bad URL. This is a problem, because it kills my cluster, which I’d like to guarantee will live forever

Here’s the code: (uses fibers.promise)

function openConnection(dest, port, contentType, method, throwErrorOnBadStatus)
{
  "use strict";
  assert.ok(dest, "generalUtilities.openConnection: dest is null");
  //dest = dest.replace('//','/');
  console.log('opening connection: ' + dest + " contentType: " + contentType);
  var prom = promise(),
    errProm = promise(),
    ar = [],
    urlParts = url.parse(dest),
    httpClient,
    req,
    got,
    res;
  //console.log('urlParts.port: ' + urlParts.port);
  if (port) {
    urlParts.port = port;
  } else if (!urlParts.port) {
    urlParts.port = 80;
  }
  if (contentType) {
    urlParts.accept = contentType;
  } else {
    urlParts.contentType = 'text/html';
  }
  if (!urlParts.method) {
    if (method) {
      urlParts.method = method;
    } else {
      urlParts.method = 'GET';
    }
  }
  try {
    httpClient = http.createClient(urlParts.port, urlParts.hostname);
    req = httpClient.request(urlParts.method, urlParts.path, urlParts);
  //console.log('req: ' + req);
  //if (req.connection) {
  //  req.connection.setTimeout(HTTP_REQUEST_TIMEOUT);
  //}
  //else {
  //  throw new Error ("No Connection Established!");
  //}
      req.end();
    req.on('response', prom);
    req.on('error', errProm);
    got = promise.waitAny(prom, errProm);
    if (got === errProm) {
      //assert.ifError(errProm.get(), HTTP_REQUEST_TIMEOUT_MSG + dest);
      throw new Error(HTTP_REQUEST_TIMEOUT_MSG + dest + ': ' + got.get());
    }
    res = prom.get();
    ar.res = res;
    ar.statusCode = res.statusCode;
    if (ar.statusCode >= 300 && throwErrorOnBadStatus) {
      assert.ifError("page not found!");
    }
    return ar;
  }
  catch (err) {
    console.log(err);
  }
}

and here’s how I tested it

var promise = require('fibers-promise');
var gu = require("../src/utils/generalutilities.js");

var brokenSite = 'http://foo.bar.com:94//foo.js';
promise.start(function () {
  try {
    gu.openConnection(brokenSite, null, null, "GET", true);
  }
  catch (err) {
    console.log('got: ' + err);
  }
});

When I run this code I get:

Error: getaddrinfo ENOENT. It is never caught in the try catch

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:38:40+00:00Added an answer on June 2, 2026 at 6:38 am

    It works for me, when supplying an error handler for the request:

    req.on('error', errorHandler);
    

    I see that you also do that, but you set it after issuing

    req.end();
    

    Could you try issuing the end() after you attached the error handler?

    As a side note, I really recommend request, as it handles issues like this with sensible defaults. It’s really a breeze to work with.

    Edit: Here is a simple example showing that attaching an error handler lets me handle ENOENT/ENOTFOUND errors:

    var http = require('http');
    
    var req = http.request({hostname: 'foo.example.com'}, function(err, res) {
        if(err) return console.error(err);
        console.log('got response!');
    });
    
    req.on('error', function(err) {
        console.error('error!', err);
    });
    

    Another piece of valuable information: I’m not sure how it fits in with fibers, but in general, you should never throw in nodejs asynchronous code. It rarely works the way you want. Instead, use the standard practice of passing any error as the first parameter to the next callback, and handle the error where it makes sense (usually, high up in the call chain where you can do something sensible with it).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've installed NVM for node.js using the instructions from this post: http://www.backdrifter.com/2011/02/18/using-nvm-and-npm-to-manage-node-js/ When I
I am using node-http-proxy . However, in addition to relaying HTTP requests, I also
I'm using node.js to make a http request to a host. I found the
I'm using node.js and express to handle HTTP requests and responses. By using the
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
From http://metacpan.org/pod/XML::LibXML::Node : find evaluates the XPath 1.0 expression using the current node as
I'm using node, express and connect for a simple app and have basic HTTP
I'm following this tutorial: http://vimeo.com/17442755 . It teaches you how to upload files using
I just installed mongodb from Synaptic (Using ubuntu 11.10). I'm following this tutorial: http://howtonode.org/express-mongodb
I have written a http server using node js var sys = require(sys), http

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.