From 1e9e2bda0feaf45abe3c1a62a6ab397e5d9ddc8d Mon Sep 17 00:00:00 2001 From: atlowChemi Date: Mon, 8 Jan 2024 00:05:26 +0200 Subject: [PATCH] fix: non-object error in abort throws bad error --- index-fetch.js | 4 +++- test/fetch/issue-2242.js | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) 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..462f4bb4453 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('http://localhost', { + signal: AbortSignal.abort('Already aborted') + }), + /Already aborted/ + ) +})