Skip to content

Commit

Permalink
fix: forward error reason to fetch controller (#2172)
Browse files Browse the repository at this point in the history
* fix: forward error reason to fetch controller

Fixes #2171

* fix: update spec text

* fixup
  • Loading branch information
KhafraDev committed Jun 27, 2023
1 parent 23e62c4 commit e66e2c6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/fetch/index.js
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
@@ -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
}
)
})

0 comments on commit e66e2c6

Please sign in to comment.