javascript 请求管道上的错误处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20196223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error handling on request piping
提问by ioncreature
I wrote simple proxy on nodejs and it looks like
我在 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 );
});
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?
我该如何处理此类错误?
回答by Tom Grant
Looking at the docs (https://github.com/mikeal/request) you should be able to do something along the following lines:
查看文档(https://github.com/mikeal/request),您应该能够按照以下方式做一些事情:
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
});
回答by bschwagg
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.
如果您捕获 ECONNREFUSED 的未捕获异常,请确保重新启动您的进程。我在测试中看到,如果您忽略异常并尝试重新连接,则套接字会变得不稳定。
Here's a great overview: http://shapeshed.com/uncaught-exceptions-in-node/
这是一个很好的概述: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
}
回答by Haroldo_OK
You should actually try to prevent the ECONNREFUSED exception from becoming uncaught:
您实际上应该尝试防止 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 );
});
If you get an actual uncaught exception, then you should just let the application die.
如果您遇到实际未捕获的异常,那么您应该让应用程序终止。