如何处理ETIMEDOUT错误? [英] How to handle ETIMEDOUT error?

查看:749
本文介绍了如何处理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);
});
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / pre>

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



究竟可以导致此错误?超时?

解决方案

这是由于在给定时间内没有收到您的请求响应(由 timeout 请求模块选项)。



首先要捕获该错误,您需要在错误,所以未处理的错误不会被抛出: out.on('error',function(err){/ * handle errors here * /})。更多的解释 here



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



如果要再次请求文件,建议使用节点重试节点退回模块。它使事情变得更简单。



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


How to handle etimedout error on this call ?

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

There are a way to wait for longer? or to request the remote file again?

What exactly can cause this error? Timeout only?

解决方案

This is caused when your request response is not received in given time(by timeout request module option).

Basically to catch that error first, you need to register a handler on error, so the unhandled error won't be thrown anymore: out.on('error', function (err) { /* handle errors here */ }). Some more explanation here.

In the handler you can check if the error is ETIMEDOUT and apply your own logic: if (err.message.code === 'ETIMEDOUT') { /* apply logic */ }.

If you want to request for the file again, I suggest using node-retry or node-backoff modules. It makes things much simpler.

If you want to wait longer, you can set timeout option of request yourself. You can set it to 0 for no timeout.

这篇关于如何处理ETIMEDOUT错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆