Error handling on request piping

19,198

Solution 1

Looking at the docs (https://github.com/mikeal/request) you should be able to do something along the following lines:

You can use the optional callback argument on request, for example:

app.all( '/proxy/*', function( req, res ){
  req.pipe( request({
      url: config.backendUrl + req.params[0],
      qs: req.query,
      method: req.method
  }, function(error, response, body){
    if (error.code === 'ECONNREFUSED'){
      console.error('Refused connection');
    } else { 
      throw error; 
    }
  })).pipe( res );
});

Alternatively, you can catch an uncaught exception, with something like the following:

process.on('uncaughtException', function(err){
  console.error('uncaughtException: ' + err.message);
  console.error(err.stack);
  process.exit(1);             // exit with error
});

Solution 2

If you catch the uncaught exception for ECONNREFUSED make sure to restart your process. I saw in testing that the socket becomes unstable if you ignore the exception and simply try to re-connect.

Here's a great overview: http://shapeshed.com/uncaught-exceptions-in-node/

I ended up using the "forever" tool to restart my node process, with the following code:

process.on('uncaughtException', function(err){
//Is this our connection refused exception?
  if( err.message.indexOf("ECONNREFUSED") > -1 )
  {
    //Safer to shut down instead of ignoring
    //See: http://shapeshed.com/uncaught-exceptions-in-node/
    console.error("Waiting for CLI connection to come up. Restarting in 2 second...");
    setTimeout(shutdownProcess, 2000); 
  }
  else
  {
   //This is some other exception.. 
   console.error('uncaughtException: ' + err.message);
   console.error(err.stack);
   shutdownProcess();
  }
});

//Desc: Restarts the process. Since forever is managing this process it's safe to shut down
//      it will be restarted.  If we ignore exceptions it could lead to unstable behavior.
//      Exit and let the forever utility restart everything
function shutdownProcess()
{
  process.exit(1); //exit with error
}

Solution 3

You should actually try to prevent the ECONNREFUSED exception from becoming uncaught:

var request = require( 'request' );
app.all( '/proxy/*', function( req, res ){
    req.pipe( request({
        url: config.backendUrl + req.params[0],
        qs: req.query,
        method: req.method
    }))
    .on('error', err => {
        const msg = 'Error on connecting to the webservice.';
        console.error(msg, err);
        res.status(500).send(msg);
    })
    .pipe( res );
});

If you get an actual uncaught exception, then you should just let the application die.

Share:
19,198

Related videos on Youtube

ioncreature
Author by

ioncreature

JavaScript developer, mostly on NodeJS

Updated on September 15, 2022

Comments

  • ioncreature
    ioncreature over 1 year

    I wrote simple proxy on nodejs and it looks like

    var request = require( 'request' );
    app.all( '/proxy/*', function( req, res ){
        req.pipe( request({
            url: config.backendUrl + req.params[0],
            qs: req.query,
            method: req.method
        })).pipe( res );
    });
    

    It works fine if remote host is available, but if remote host is unavailable the whole node server crashes with unhandled exception

    stream.js:94                                               
          throw er; // Unhandled stream error in pipe.         
                ^                                              
    Error: connect ECONNREFUSED                                
        at errnoException (net.js:901:11)                      
        at Object.afterConnect [as oncomplete] (net.js:892:19) 
    

    How can I handle such errors?