Retry on etimedout

My requests work well but sometimes i get a timeout error “etimedout”, how can i retry these requests ?

Hello @neoxedneox welcome to the community :wave:

Arguably the best approach here might be to increase the timeout limit and try to prevent that error from happening in the first place.
However, in case that’s not an option, there are few different options to look at.

The main problem is that inside Postman you won’t be able to reach the “tests” of your request and that might prevent you from making an assertion to reset the request because the request will throw an error.

^ An error will cause the collection runner to stop

Option 1) You can use Newman, as Newman will not abort the run if there’s an error and the tests tab of the request will be reached.

You can add the following snippet in your collection’s test tab:

if(!pm.response.code){
    console.log(request.name, "Failed, retrying now")
    postman.setNextRequest(request.name)
}

Basically, this will check if pm.response.code exists and if it doesn’t it will re-run the request
A thing to keep in mind, you may end up in an endless loop in case your requests error out every time, so it’s good to add some sort of retries amount to prevent executing the collection endlessly

Option 2) You can try to implement the suggestion made in that thread, even though it is regarding ECONNRESET, pretty much the same approach should be suitable for ECONNTIMEDOUT

Hello @security-cosmologist :wave:,

Thank you for your valuable answer and options, I think to do it with newman and implement your snippet because the workflow is designed to stop on certain values. I will update later after testing :smiley:

@security-cosmologist

if(!pm.response.code){
    console.log(request.name, "Failed, retrying now")
    postman.setNextRequest(request.name)
}

Take care when using this snippet. There’s a chance of infinitely repeating code.

Since all this is doing is verifying that the code isn’t a truthy response, it could repeat forever if there’s something wrong.

I like to add a count to my repeat calls, just to prevent issues like that from happening like this:

let countVar = 0;
if (pm.environment.has("countVar")) {
    countVar = pm.environment.get("countVar");
}

if(!pm.response.code && countVar < 10){
    console.log(request.name, "Failed, retrying now")
    postman.setNextRequest(request.name)
    pm.environment.set("countVar", ++countVar);
} else {
    pm.test("Good Response", function(){
        pm.response.to.have.status(200);
    });
    pm.environment.unset("countVar");
}

In this example, I test the response 10 times. If it fails 10 times in a row, the code will proceed to the next API call, and will throw a test failure.