Skip to content

Commit

Permalink
Merge pull request #1195 from mwolson/retry-crossdomain-errors
Browse files Browse the repository at this point in the history
Allow crossDomain errors to be retried (#1194)
  • Loading branch information
kornelski committed Mar 18, 2017
2 parents 99db1d4 + 765de56 commit acaf84d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
9 changes: 9 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ You can also use the `.query()` method for HEAD requests. The following will pro

SuperAgent will automatically serialize JSON and forms. If you want to send the payload in a custom format, you can replace the built-in serialization with `.serialize()` method.

## Retrying requests

When given the `.retry()` method, SuperAgent will automatically retry requests, if they fail in a way that is transient or could be due to a flaky Internet connection. `.retry()` takes an optional argument which is the maximum number of times to retry failed requests; the default is 3 times.

request
.get('http://example.com/search')
.retry(2)
.end(callback);

## Setting Accept

In a similar fashion to the `.type()` method it is also possible to set the `Accept` header via the short hand method `.accept()`. Which references `request.types` as well allowing you to specify either the full canonicalized MIME type name as `type/subtype`, or the extension suffix form as "xml", "json", "png", etc. for convenience:
Expand Down
3 changes: 2 additions & 1 deletion lib/should-retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ module.exports = function shouldRetry(err, res) {
if (res && res.status && res.status >= 500) return true;
// Superagent timeout
if (err && 'timeout' in err && err.code == 'ECONNABORTED') return true;
if (err && 'crossDomain' in err) return true;
return false;
};
};
16 changes: 16 additions & 0 deletions test/client/xdomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,21 @@ describe('xdomain', function(){
next();
});
});

it('should handle x-domain failure after repeat attempts', function(next){
request
.get('//tunne127.com')
.retry(2)
.end(function(err, res){
try {
assert(err, 'error missing');
assert(err.crossDomain, 'not .crossDomain');
assert.equal(2, err.retries, 'expected an error with .retries');
next();
} catch(err) {
next(err);
}
});
});
}
});

0 comments on commit acaf84d

Please sign in to comment.