From d428a349a1a5c3682291de39aaed593a7e73b943 Mon Sep 17 00:00:00 2001 From: Mike Olson Date: Fri, 17 Mar 2017 18:40:33 -0700 Subject: [PATCH] Allow crossDomain errors to be retried (#1194) When using the retry() functionality on a bad connection, the browser can sometimes fail during a CORS OPTIONS request, which currently doesn't get retried. Superagent throws an error with crossDomain: true property in this case. We should retry instead, since the next attempt might succeed. --- lib/should-retry.js | 3 ++- test/client/xdomain.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/should-retry.js b/lib/should-retry.js index c30a5ad0e..ce0af386c 100644 --- a/lib/should-retry.js +++ b/lib/should-retry.js @@ -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; -}; \ No newline at end of file +}; diff --git a/test/client/xdomain.js b/test/client/xdomain.js index 9b80ad5b9..5b2f83e10 100644 --- a/test/client/xdomain.js +++ b/test/client/xdomain.js @@ -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); + } + }); + }); } });