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

查看:39
本文介绍了如何处理 ETIMEDOUT 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理这次调用的超时错误?

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?

推荐答案

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

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

基本上首先要捕获该错误,您需要在 error 上注册一个处理程序,这样就不会再抛出未处理的错误:out.on('error', function (err) {/* 在这里处理错误 */}).更多解释这里.

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.

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

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

如果你想再次请求文件,我建议使用node-retrynode-backoff 模块.它让事情变得更简单.

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

如果你想等待更长时间,你可以设置timeout选项要求自己.您可以将其设置为 0 以表示没有超时.

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天全站免登陆