diff --git a/index.js b/index.js index 7e8831ceeea..7c0c8adcd6c 100644 --- a/index.js +++ b/index.js @@ -106,7 +106,10 @@ if (util.nodeMajor > 16 || (util.nodeMajor === 16 && util.nodeMinor >= 8)) { try { return await fetchImpl(...arguments) } catch (err) { - Error.captureStackTrace(err, this) + if (typeof err === 'object') { + Error.captureStackTrace(err, this) + } + throw err } } diff --git a/test/fetch/issue-2242.js b/test/fetch/issue-2242.js new file mode 100644 index 00000000000..fe704123e9a --- /dev/null +++ b/test/fetch/issue-2242.js @@ -0,0 +1,8 @@ +'use strict' + +const { test } = require('tap') +const { fetch } = require('../..') + +test('fetch with signal already aborted', async (t) => { + await t.rejects(fetch('http://localhost', { signal: AbortSignal.abort('Already aborted') }), 'Already aborted') +})