Skip to content Skip to sidebar Skip to footer

Error Handling On Request Piping

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.backendU

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 everythingfunctionshutdownProcess()
{
  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.

Post a Comment for "Error Handling On Request Piping"