请求管道处理错误

Error handling on request piping

本文关键字:错误 处理 管道 请求      更新时间:2023-10-21

我在nodejs上写了一个简单的代理,它看起来像

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 );
});

如果远程主机可用,它可以正常工作,但如果远程主机不可用,则整个节点服务器会因未处理的异常而崩溃

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) 

我该如何处理此类错误?

查看文档(https://github.com/mikeal/request)你应该能够沿着以下路线做一些事情:

您可以根据请求使用可选的回调参数,例如:

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 );
});

或者,您可以捕获一个未捕获的异常,如下所示:

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

如果捕获到ECONNREFUSED的未捕获异常,请确保重新启动进程。我在测试中看到,如果忽略异常并简单地尝试重新连接,套接字就会变得不稳定。

以下是一个很好的概述:http://shapeshed.com/uncaught-exceptions-in-node/

我最终使用了"永远"工具来重新启动我的节点进程,代码如下:

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
}

您实际上应该尝试防止ECONNREFUSED异常变得未捕获:

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 );
});

如果您得到一个实际的未捕获异常,那么您应该让应用程序终止。