如何处理ETIMEDOUT错误?

如何处理此通话的etimedout错误?

var remotePath = "myremoteurltocopy" var localStream = fs.createWriteStream("myfil");; var out = request({ uri: remotePath }); out.on('response', function (resp) { if (resp.statusCode === 200) { out.pipe(localStream); localStream.on('close', function () { copyconcurenceacces--; console.log('aftercopy'); callback(null, localFile); }); } else callback(new Error("No file found at given url."), null); }) 

有办法等待更长的时间? 或再次请求远程文件?

究竟会导致这个错误? 只有超时?

这是在给定时间内未收到请求响应(由timeout请求模块选项)引起的。

基本上,首先要捕捉错误,你需要注册一个error的处理程序,所以不会抛出未处理的错误: out.on('error', function (err) { /* handle errors here */ }) 。 这里有更多的解释。

在处理程序中,您可以检查错误是否为ETIMEDOUT并应用您自己的逻辑: if (err.message.code === 'ETIMEDOUT') { /* apply logic */ }

如果你想再次请求文件,我build议使用节点重试或节点退避模块。 它使事情变得更简单。

如果你想等待更长的时间,你可以设置自己的timeout选项 。 您可以将其设置为0,以免超时。

我们可以查看错误对象的属性code ,提到可能的系统错误,并在ETIMEDOUTnetworking调用失败的情况下, ETIMEDOUT采取行动。

 if (err.code === 'ETIMEDOUT') { console.log('My dish error: ', util.inspect(err, { showHidden: true, depth: 2 })); }