[英]Catching ECONNRESET error from spawned Node process
I am using the sftps NPM to connect to a SFTP server using username/password authentication and upload a file. 我正在使用sftps NPM通过用户名/密码身份验证连接到SFTP服务器并上传文件。 This works beautifully when successful;
成功时,它可以很好地工作; however, if provided invalid authentication information, after several minutes it emits an
ECONNRESET
error which crashes my entire application. 但是,如果提供了无效的身份验证信息,则几分钟后它将发出
ECONNRESET
错误,该错误使我的整个应用程序崩溃。
Looking at the source of the sftps module, it appears to use child_process.spawn
to run the shell commands, and looks like it should be capturing any errors gracefully: 查看sftps模块的源代码,它似乎使用
child_process.spawn
来运行shell命令,并且看起来它应该优雅地捕获任何错误:
var sftp = spawn(shellCmd, shellOpts);
var data = "";
var error = "";
sftp.stdout.on('data', function (res) {
data += res;
});
sftp.stderr.on('data', function (res) {
error += res;
});
function finished(err) {
error = error.split('\n').filter(function(line) {
if (/^Connected to /.test(line)) return false;
return true;
}).join('\n');
data = data.split('\n').filter(function(line) {
if (/^sftp> /.test(line)) return false;
return true;
}).join('\n');
if (callback) {
if (err) {
callback(err, { error: error || null, data: data });
} else if (error) {
callback(null, { error: error, data: data });
} else {
callback(null, { error: null, data: data });
}
callback = null;
}
}
sftp.on('error', finished);
sftp.on('exit', function (code) {
if (code === 0) {
finished();
} else {
finished('Nonzero exit code: ' + code);
}
});
sftp.stdin.write(cmdString, 'utf8');
It looks like it has hooked into the stderr
stream as well as capturing the error
and exit
events on the sub-process. 看起来它已经挂接到
stderr
流中,并且捕获了子进程上的error
和exit
事件。
What else can I do to capture this ECONNRESET
error and log it without killing the application? 我还能采取什么措施来捕获此
ECONNRESET
错误并将其记录下来而又不终止应用程序?
Don't know if you're having the same problem that I was having, but I eventually solved it by attaching an event to stdin.on("error")
. 不知道您是否遇到了与我同样的问题,但最终我通过将事件附加到
stdin.on("error")
来解决了这个问题。 You could try this to see if that's the problem: 您可以尝试这样做,看看是否是问题所在:
sftp.stdin.on("error", function (e)
{
console.log("STDIN ON ERROR");
console.log(e);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.