Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: forward error reason to fetch controller #2172

Merged
merged 3 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
)
Expand Down
25 changes: 25 additions & 0 deletions test/fetch/issue-2171.js
Original file line number Diff line number Diff line change
@@ -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
}
)
})