From 48e7096dbf271285f88232beae45a3fb55820ef7 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Sat, 23 Aug 2025 21:15:45 +0200 Subject: [PATCH 1/3] fetch: remove promise in exported fetch --- index.js | 10 ++++------ test/fetch/client-error-stack-trace.js | 12 ++++++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 537958fadf8..1ffeb9a3aff 100644 --- a/index.js +++ b/index.js @@ -117,16 +117,14 @@ module.exports.setGlobalDispatcher = setGlobalDispatcher module.exports.getGlobalDispatcher = getGlobalDispatcher const fetchImpl = require('./lib/web/fetch').fetch -module.exports.fetch = async function fetch (init, options = undefined) { - try { - return await fetchImpl(init, options) - } catch (err) { + +module.exports.fetch = function fetch (init, options = undefined) { + return fetchImpl(init, options).catch(err => { if (err && typeof err === 'object') { Error.captureStackTrace(err) } - throw err - } + }) } module.exports.Headers = require('./lib/web/fetch/headers').Headers module.exports.Response = require('./lib/web/fetch/response').Response diff --git a/test/fetch/client-error-stack-trace.js b/test/fetch/client-error-stack-trace.js index 146443bc077..6f6aa53f986 100644 --- a/test/fetch/client-error-stack-trace.js +++ b/test/fetch/client-error-stack-trace.js @@ -14,7 +14,11 @@ test('FETCH: request errors and prints trimmed stack trace', async (t) => { try { await fetch('http://a.com') } catch (error) { - assert.ok(error.stack.includes(`at async TestContext. (${__filename}`)) + const stackLines = error.stack.split('\n') + assert.ok(stackLines[0].includes('TypeError: fetch failed')) + assert.ok(stackLines[1].includes('undici/index.js')) + assert.ok(stackLines[2].includes('at process.processTicksAndRejections')) + assert.ok(stackLines[3].includes(`at async TestContext. (${__filename}`)) } }) @@ -22,6 +26,10 @@ test('FETCH-index: request errors and prints trimmed stack trace', async (t) => try { await fetchIndex('http://a.com') } catch (error) { - assert.ok(error.stack.includes(`at async TestContext. (${__filename}`)) + const stackLines = error.stack.split('\n') + assert.ok(stackLines[0].includes('TypeError: fetch failed')) + assert.ok(stackLines[1].includes('undici/index-fetch.js')) + assert.ok(stackLines[2].includes('at process.processTicksAndRejections')) + assert.ok(stackLines[3].includes(`at async TestContext. (${__filename}`)) } }) From 64b12ab7ffd3f8f2533f975a290b16a0c6b4b3ce Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Sat, 23 Aug 2025 21:28:03 +0200 Subject: [PATCH 2/3] fix windows --- test/fetch/client-error-stack-trace.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/fetch/client-error-stack-trace.js b/test/fetch/client-error-stack-trace.js index 6f6aa53f986..b72aaa2dcad 100644 --- a/test/fetch/client-error-stack-trace.js +++ b/test/fetch/client-error-stack-trace.js @@ -2,6 +2,7 @@ const { test } = require('node:test') const assert = require('node:assert') +const { sep } = require('node:path') const { fetch, setGlobalDispatcher, Agent } = require('../..') const { fetch: fetchIndex } = require('../../index-fetch') @@ -16,7 +17,7 @@ test('FETCH: request errors and prints trimmed stack trace', async (t) => { } catch (error) { const stackLines = error.stack.split('\n') assert.ok(stackLines[0].includes('TypeError: fetch failed')) - assert.ok(stackLines[1].includes('undici/index.js')) + assert.ok(stackLines[1].includes(`undici${sep}index.js`)) assert.ok(stackLines[2].includes('at process.processTicksAndRejections')) assert.ok(stackLines[3].includes(`at async TestContext. (${__filename}`)) } @@ -28,7 +29,7 @@ test('FETCH-index: request errors and prints trimmed stack trace', async (t) => } catch (error) { const stackLines = error.stack.split('\n') assert.ok(stackLines[0].includes('TypeError: fetch failed')) - assert.ok(stackLines[1].includes('undici/index-fetch.js')) + assert.ok(stackLines[1].includes(`undici${sep}index-fetch.js`)) assert.ok(stackLines[2].includes('at process.processTicksAndRejections')) assert.ok(stackLines[3].includes(`at async TestContext. (${__filename}`)) } From 49fd3278447ea5d9fd6d7b4bfcfcab40415f633e Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Sun, 24 Aug 2025 16:31:40 +0200 Subject: [PATCH 3/3] Refactor fetch function parameters and error handling --- index-fetch.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index-fetch.js b/index-fetch.js index 711d7e8a1e4..8f5bb6ceae2 100644 --- a/index-fetch.js +++ b/index-fetch.js @@ -4,8 +4,8 @@ const { getGlobalDispatcher, setGlobalDispatcher } = require('./lib/global') const EnvHttpProxyAgent = require('./lib/dispatcher/env-http-proxy-agent') const fetchImpl = require('./lib/web/fetch').fetch -module.exports.fetch = function fetch (resource, init = undefined) { - return fetchImpl(resource, init).catch((err) => { +module.exports.fetch = function fetch (init, options = undefined) { + return fetchImpl(init, options).catch(err => { if (err && typeof err === 'object') { Error.captureStackTrace(err) }