From faa3d8942af41c5865df549268c6188fa5d4ddc8 Mon Sep 17 00:00:00 2001 From: Khafra Date: Mon, 26 Jun 2023 21:31:02 -0400 Subject: [PATCH] fix: forward error reason to fetch controller (#2172) * fix: forward error reason to fetch controller Fixes https://github.com/nodejs/undici/issues/2171 * fix: update spec text * fixup --- lib/fetch/index.js | 13 +++++++------ test/fetch/issue-2171.js | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 test/fetch/issue-2171.js diff --git a/lib/fetch/index.js b/lib/fetch/index.js index 7388da51ffc..f69371a941d 100644 --- a/lib/fetch/index.js +++ b/lib/fetch/index.js @@ -180,14 +180,15 @@ async function fetch (input, init = {}) { // 1. Set locallyAborted to true. locallyAborted = true - // 2. Abort the fetch() call with p, request, responseObject, + // 2. Assert: controller is non-null. + assert(controller != null) + + // 3. Abort controller with requestObject’s signal’s abort reason. + controller.abort(requestObject.signal.reason) + + // 4. Abort the fetch() call with p, request, responseObject, // and requestObject’s signal’s abort reason. abortFetch(p, request, responseObject, requestObject.signal.reason) - - // 3. If controller is not null, then abort controller. - if (controller != null) { - controller.abort() - } }, { once: true } ) diff --git a/test/fetch/issue-2171.js b/test/fetch/issue-2171.js new file mode 100644 index 00000000000..b04ae0e6c38 --- /dev/null +++ b/test/fetch/issue-2171.js @@ -0,0 +1,25 @@ +'use strict' + +const { fetch } = require('../..') +const { DOMException } = require('../../lib/fetch/constants') +const { once } = require('events') +const { createServer } = require('http') +const { test } = require('tap') + +test('error reason is forwarded - issue #2171', { skip: !AbortSignal.timeout }, async (t) => { + const server = createServer(() => {}).listen(0) + + t.teardown(server.close.bind(server)) + await once(server, 'listening') + + const timeout = AbortSignal.timeout(100) + await t.rejects( + fetch(`http://localhost:${server.address().port}`, { + signal: timeout + }), + { + name: 'TimeoutError', + code: DOMException.TIMEOUT_ERR + } + ) +})