JavaScript How can I catch an EHOSTDOWN socket error when creating an http request with iojs?

I have a simple iojs http server which communicates with another http backend on my development machine. Now my ip has changed and the backend request won't work due to a wrong ip. I have (or at least thought so) error management in place however the server crashes in some situations due to an unhandled exception:

When doing 2 subsequent requests the first one "hangs" and then the second request crashes the server:

events.js:141
      throw er; // Unhandled 'error' event
            ^
Error: connect EHOSTDOWN 192.168.1.11:80 - Local (192.168.1.10:54125)
    at Object.exports._errnoException (util.js:846:11)
    at exports._exceptionWithHostPort (util.js:869:20)
    at connect (net.js:840:14)
    at lookupAndConnect (net.js:933:5)
    at Socket.connect (net.js:902:5)
    at Agent.exports.connect.exports.createConnection (net.js:61:35)
    at Agent.createSocket (_http_agent.js:177:16)
    at Agent.addRequest (_http_agent.js:147:23)
    at new ClientRequest (_http_client.js:132:16)
    at Object.exports.request (http.js:30:10)

The error in question won't trigger backRequest.on("error", errFn) and it is not a standard error as in function(err, response, body). It can't be catched using try...catch.

How can i catch and gracefully handle this error?

I have dugged a bit into net.js/http.js. When emitting a socket error manually:

backendRequest.on("socket", function(socket) {
    socket.emit("error", "DIE!");
});

it is handled by the normal error handler. The EHOSTDOWN error however is not. The reason for this seems to be that the socket error handler is only installed on the next tick after Socket.connect() (see https://github.com/joyent/node/blob/master/lib/_http_client.js#L498).

"My" error however is triggered directly upon connect() (must have something to do with the first connection still trying to find the host).

I can gracefully catch that error using a custom Agent.createConnection:

var socket = net.createConnection(options);
// install error handler immediately
socket.on("error", function(err) {
    console.log("AHA", err);
});
return socket;

however that seems very verbose and bulky. Is that truly the best way to do that? The error seems to be not that special and seems to be unhandled in the core libs. Why is that so and why is the socket error handler only installed upon nextTick?

How would you properly catch that error? Is the agent the correct way?

A "full" example to play with can be found here: http://pastebin.com/jaCUPaHX

Answer:1

I was wondering if typical javascript topology chart libraries can be easily customized to perform some action when I hover or click on the node object? The libraries I'm currently considering are D3....

I was wondering if typical javascript topology chart libraries can be easily customized to perform some action when I hover or click on the node object? The libraries I'm currently considering are D3....

  1. javascript chart library comparison
  2. javascript chart library 2019
  3. javascript chart library react
  4. javascript chart library free
  5. javascript chart library open source
  6. javascript chart library 2020
  7. javascript chart library candlestick
  8. javascript chart library performance
  9. javascript chart library large dataset
  10. javascript chart library
  11. javascript chart library 2018
  12. javascript chart library d3
  13. javascript chart library github
  14. javascript chart library big data
  15. javascript chart library svg
  16. javascript chart library wiki
  17. javascript chart library 2019 free
  18. javascript chart library timeline
  19. javascript chart library best
  20. javascript chart library zoom

this might be a very basic question. But I would like to know how I can select specific elements out of an array. I have an array with 100 elements: var cubes = [element1, element2, element3 ...] ...

this might be a very basic question. But I would like to know how I can select specific elements out of an array. I have an array with 100 elements: var cubes = [element1, element2, element3 ...] ...

  1. select specific numbers in excel
  2. select specific numbers
  3. select specific row numbers in excel
  4. select specific row numbers sql
  5. sql select specific numbers
  6. how to select specific numbers in a cell

I want to use the Perf stuff. It seems that if I do: var React = require('react/addons'); in my own modules then libraries (react-bootstrap, react-router etc.) end up using another copy of plain ...

I want to use the Perf stuff. It seems that if I do: var React = require('react/addons'); in my own modules then libraries (react-bootstrap, react-router etc.) end up using another copy of plain ...

I'm trying to rewrite a C# function that uses yield and recursion to pull out instances of a class out of nested lists into a single list. This is the C# function: public static IEnumerable<...

I'm trying to rewrite a C# function that uses yield and recursion to pull out instances of a class out of nested lists into a single list. This is the C# function: public static IEnumerable<...

  1. typescript function in interface
  2. typescript function in class
  3. typescript function with return type
  4. typescript function with properties
  5. typescript function with any number of arguments
  6. typescript function with any arguments
  7. typescript function with parameters
  8. typescript function with optional parameter
  9. typescript function in object
  10. typescript function in enum
  11. typescript function with generics
  12. typescript function with variable number of arguments
  13. typescript function with default value
  14. typescript function with callback
  15. typescript function with return
  16. typescript function with any parameters
  17. typescript function with arguments
  18. typescript function with multiple parameters
  19. typescript function with generic type
  20. typescript function with different parameters