@zeit/fetch-retry
Advanced tools
Comparing version 3.0.0 to 4.0.0
@@ -35,2 +35,3 @@ const retry = require('async-retry'); | ||
const {method = 'GET'} = opts; | ||
const isRetry = attempt < retryOpts.retries; | ||
try { | ||
@@ -40,3 +41,3 @@ // this will be retried | ||
debug('status %d', res.status); | ||
if (res.status >= 500 && res.status < 600) { | ||
if (res.status >= 500 && res.status < 600 && isRetry) { | ||
const err = new Error(res.statusText); | ||
@@ -50,3 +51,3 @@ err.code = err.status = err.statusCode = res.status; | ||
} catch (err) { | ||
debug(`${method} ${url} error (${err.status}). ${attempt < MAX_RETRIES ? 'retrying' : ''}`, err); | ||
debug(`${method} ${url} error (${err.status}). ${isRetry ? 'retrying' : ''}`, err); | ||
throw err; | ||
@@ -53,0 +54,0 @@ } |
{ | ||
"name": "@zeit/fetch-retry", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"devDependencies": { | ||
@@ -5,0 +5,0 @@ "jest": "^21.2.1", |
@@ -1,2 +0,2 @@ | ||
# fetch-retry | ||
# fetch-retry [![CircleCI](https://circleci.com/gh/zeit/fetch-retry.svg?style=svg)](https://circleci.com/gh/zeit/fetch-retry) | ||
@@ -3,0 +3,0 @@ A layer on top of `fetch` (via [node-fetch](https://www.npmjs.com/package/node-fetch)) |
32
test.js
@@ -31,3 +31,3 @@ const {createServer} = require('http'); | ||
test('fails on >MAX_RETRIES', async () => { | ||
test('resolves on >MAX_RETRIES', async () => { | ||
const server = createServer((req, res) => { | ||
@@ -41,10 +41,6 @@ res.writeHead(500); | ||
const {port} = server.address(); | ||
try { | ||
await retryFetch(`http://127.0.0.1:${port}`); | ||
} catch (err) { | ||
expect(await err.status).toBe(500); | ||
server.close(); | ||
return resolve(); | ||
} | ||
reject(new Error('must fail')); | ||
const res = await retryFetch(`http://127.0.0.1:${port}`); | ||
expect(res.status).toBe(500); | ||
server.close(); | ||
return resolve(); | ||
}); | ||
@@ -68,13 +64,9 @@ server.on('error', reject); | ||
const {port} = server.address(); | ||
try { | ||
await retryFetch(`http://127.0.0.1:${port}`, opts); | ||
} catch (err) { | ||
expect(opts.onRetry.mock.calls.length).toBe(3); | ||
expect(opts.onRetry.mock.calls[0][0]).toEqual(err); | ||
expect(opts.onRetry.mock.calls[0][1]).toEqual(opts); | ||
expect(await err.status).toBe(500); | ||
server.close(); | ||
return resolve(); | ||
} | ||
reject(new Error('must fail')); | ||
const res = await retryFetch(`http://127.0.0.1:${port}`, opts); | ||
expect(opts.onRetry.mock.calls.length).toBe(2); | ||
expect(opts.onRetry.mock.calls[0][0]).toBeInstanceOf(Error); | ||
expect(opts.onRetry.mock.calls[0][1]).toEqual(opts); | ||
expect(res.status).toBe(500); | ||
server.close(); | ||
return resolve(); | ||
}); | ||
@@ -81,0 +73,0 @@ server.on('error', reject); |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
92501
6
118