diff --git a/index-fetch.js b/index-fetch.js index ba31a65f25c..db17a828c21 100644 --- a/index-fetch.js +++ b/index-fetch.js @@ -4,7 +4,9 @@ const fetchImpl = require('./lib/fetch').fetch module.exports.fetch = function fetch (resource, init = undefined) { return fetchImpl(resource, init).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 index a9db38c9b7d..ca12cc61c09 100644 --- a/test/fetch/issue-2242.js +++ b/test/fetch/issue-2242.js @@ -3,6 +3,7 @@ const { test } = require('node:test') const assert = require('node:assert') const { fetch } = require('../..') +const nodeFetch = require('../../index-fetch') test('fetch with signal already aborted', async () => { await assert.rejects( @@ -12,3 +13,12 @@ test('fetch with signal already aborted', async () => { /Already aborted/ ) }) + +test('fetch with signal already aborted (from index-fetch)', async () => { + await assert.rejects( + nodeFetch.fetch('http://localhost', { + signal: AbortSignal.abort('Already aborted') + }), + /Already aborted/ + ) +})