请求管道处理错误
Error handling on request piping
我在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 );
});
如果您得到一个实际的未捕获异常,那么您应该让应用程序终止。
相关文章:
- javascript:如何在antlr生成的Lexer中进行错误处理
- 生成pdf或其他非html文件时的错误处理
- sailsjs在创建两个模型时的错误处理
- 为什么没有调用我的流错误处理程序
- 错误处理try&catch+回调
- EventSource中的错误处理
- Angular JS中的通用Ajax错误处理-拦截器
- Sinon Mocha节点错误处理
- 如果文件不存在,fs.watch是否有错误处理程序
- Javascript初始化和错误处理
- AngularJS错误处理:根据错误数组显示错误
- JQuery 图像错误处理
- BreezeJS中央错误处理程序和自动重试
- 文件下载html与错误处理FileNotFound
- 使用mongoose保存多个文档时的错误处理
- Node.js如何知道回调的第一个参数是否是错误处理程序
- Meteor JavaScript-错误处理
- AJAX自定义错误处理代码问题
- 正在获取丢失的错误处理程序套接字消息,无法读取属性'timerID'使用Node.js和Socket.i
- 为什么不't JavaScript库使用错误处理