How to handle ETIMEDOUT error?
Asked Answered
U

5

27

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?

Unfeeling answered 13/5, 2014 at 13:41 Comment(0)
P
48

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.

Pomace answered 13/5, 2014 at 13:55 Comment(4)
I believe these node core errors have a .code or similar property that contains just 'etimedout' or similar that you can check instead of having to do a err.message.match().Ehf
Hi. THanks for your help. Can you give me some link for better understanding error handling and catch in nodejs ? I have an etimdout after few seconds.Unfeeling
Can you please tell me this error is caused by a timeout, or it can be fired with another error ?Unfeeling
@Unfeeling Updated it. It is fired by a timeout. No other error might. Although ESOCKETTIMEOUT might also be fired too, when your socket is freezed. Generally they don't fire together.Pomace
A
6

We could look at error object for a property code that mentions the possible system error and in cases of ETIMEDOUT where a network call fails, act accordingly.

if (err.code === 'ETIMEDOUT') {
    console.log('My dish error: ', util.inspect(err, { showHidden: true, depth: 2 }));
}
Arraignment answered 2/11, 2015 at 3:24 Comment(3)
It's best if you add some commentary and not just toss code out there.Tubate
I also had to use err.code to get the 'ETIMEDOUT' error code. The highest votes answer err.message.code didn't work for me. So thanks for this :)Mellon
can try this keepAliveTimeout, for more detail check belowLanitalank
L
5

In case if you are using node js, then this could be the possible solution

const express = require("express");
const app = express();
const server = app.listen(8080);
server.keepAliveTimeout = 61 * 1000;

https://medium.com/hk01-tech/running-eks-in-production-for-2-years-the-kubernetes-journey-at-hk01-68130e603d76

Lanitalank answered 2/12, 2020 at 20:26 Comment(0)
A
-1

Try switching internet networks and test again your code. I got this error and the only solution was switching to another internet.

Edit: I now know people besides me that have had this error and the solution was communicating with the ISP and ask them to chek the dns configuration because the http request were failing. So switching networks definitely could help with this.

That is why I will not delete the post. I could save people a few days of headaches (especially noobs like me).

Amylolysis answered 6/4, 2022 at 0:59 Comment(1)
This shouldn't be an answerBulbous
S
-4

Simply use a different network. Using a different network solved this issue for me within seconds. Probably because this other network had a stronger connection.

Schaffer answered 8/2, 2023 at 20:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.