From c33b7dad5de7b349bcd7ab66ed9785bd05c8867c Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 12 Oct 2020 17:57:46 -0700 Subject: [PATCH] Stop retrying when fetch throws `ERR_UNESCAPED_CHARACTERS` error This is a client/user error and should not be retried, as the same error will always be thrown again. --- index.js | 8 ++++++-- test.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 75818e4..cadbbf6 100644 --- a/index.js +++ b/index.js @@ -55,8 +55,12 @@ function setup(fetch) { return res; } } catch (err) { - const isRetry = attempt <= retryOpts.retries; - debug(`${method} ${url} error (${err.status}). ${isRetry ? 'retrying' : ''}`, err); + const isClientError = err && err.code === 'ERR_UNESCAPED_CHARACTERS'; + const isRetry = !isClientError && attempt <= retryOpts.retries; + debug(`${method} ${url} error (status = ${err.status}). ${isRetry ? 'retrying' : ''}`, err); + if (isClientError) { + return bail(err); + } throw err; } }, retryOpts); diff --git a/test.js b/test.js index 53dbbac..b7bbdc0 100644 --- a/test.js +++ b/test.js @@ -1,3 +1,4 @@ +const assert = require('assert'); const {createServer} = require('http'); const setup = require('./index'); @@ -145,3 +146,20 @@ test('stops retrying when the Retry-After header exceeds the maxRetryAfter optio server.on('error', reject); }); }); + +test('stops retrying when fetch throws `ERR_UNESCAPED_CHARACTERS` error', async () => { + const opts = { + onRetry: jest.fn(), + } + + let err; + try { + await retryFetch(`http://127.0.0.1/\u0019`, opts); + } catch (_err) { + err = _err; + } + + assert(err); + assert.equal(err.code, 'ERR_UNESCAPED_CHARACTERS'); + assert.equal(opts.onRetry.mock.calls.length, 0); +});