diff --git a/index.js b/index.js index 40009c8..b100e67 100644 --- a/index.js +++ b/index.js @@ -56,7 +56,7 @@ function requestAsEventEmitter(opts) { }); req.once('error', function (err) { - var backoff = opts.retries(++retryCount); + var backoff = opts.retries(++retryCount, err); if (backoff) { setTimeout(get, backoff, opts); return; diff --git a/readme.md b/readme.md index f720740..1fed6ef 100644 --- a/readme.md +++ b/readme.md @@ -123,7 +123,7 @@ Default: `5` Number of request retries when network errors happens. Delays between retries counts with function `Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 0). -Option accepts `function` with `retry` argument that must return delay in milliseconds (`0` return value cancels retry). +Option accepts `function` with `retry` and `error` arguments. Function must return delay in milliseconds (`0` return value cancels retry). ##### callback(error, data, response) diff --git a/test/retry.js b/test/retry.js index 877d951..d4f42d0 100644 --- a/test/retry.js +++ b/test/retry.js @@ -58,6 +58,17 @@ test('falsy value prevent retries', async t => { } }); +test('falsy value prevent retries', async t => { + try { + await got(`${s.url}/long`, {timeout: 100, retries: (iter, err) => { + t.ok(err); + return false; + }}); + } catch (err) { + t.ok(err); + } +}); + test.after('cleanup', async t => { await s.close(); });