Skip to content

Commit 81e3d84

Browse files
authored
abort request with reason if one is provided (#2592)
1 parent 62efdad commit 81e3d84

File tree

5 files changed

+49
-14
lines changed

5 files changed

+49
-14
lines changed

lib/api/abort-signal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const kSignal = Symbol('kSignal')
66

77
function abort (self) {
88
if (self.abort) {
9-
self.abort()
9+
self.abort(self[kSignal]?.reason)
1010
} else {
11-
self.onError(new RequestAbortedError())
11+
self.onError(self[kSignal]?.reason ?? new RequestAbortedError())
1212
}
1313
}
1414

test/issue-2349.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { test } = require('tap')
44
const { Writable } = require('stream')
5-
const { MockAgent, errors, stream } = require('..')
5+
const { MockAgent, stream } = require('..')
66

77
test('stream() does not fail after request has been aborted', async (t) => {
88
t.plan(1)
@@ -21,7 +21,7 @@ test('stream() does not fail after request has been aborted', async (t) => {
2121
const parts = []
2222
const ac = new AbortController()
2323

24-
setTimeout(() => ac.abort('nevermind'), 5)
24+
setTimeout(() => ac.abort(), 5)
2525

2626
try {
2727
await stream(
@@ -42,6 +42,6 @@ test('stream() does not fail after request has been aborted', async (t) => {
4242
)
4343
} catch (error) {
4444
console.log(error)
45-
t.equal(error instanceof errors.RequestAbortedError, true)
45+
t.equal(error instanceof DOMException, true)
4646
}
4747
})

test/issue-2590.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const { request } = require('..')
5+
const { createServer } = require('http')
6+
const { once } = require('events')
7+
8+
test('aborting request with custom reason', async (t) => {
9+
const server = createServer(() => {}).listen(0)
10+
11+
t.teardown(server.close.bind(server))
12+
await once(server, 'listening')
13+
14+
const timeout = AbortSignal.timeout(0)
15+
const ac = new AbortController()
16+
ac.abort(new Error('aborted'))
17+
18+
const ac2 = new AbortController()
19+
ac2.abort() // no reason
20+
21+
await t.rejects(
22+
request(`http://localhost:${server.address().port}`, { signal: timeout }),
23+
timeout.reason
24+
)
25+
26+
await t.rejects(
27+
request(`http://localhost:${server.address().port}`, { signal: ac.signal }),
28+
ac.signal.reason
29+
)
30+
31+
await t.rejects(
32+
request(`http://localhost:${server.address().port}`, { signal: ac2.signal }),
33+
{ code: 'UND_ERR_ABORTED' }
34+
)
35+
})

test/jest/mock-scope.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ it('Jest works with MockScope.delay - issue #1327', async () => {
2525
signal: ac.signal
2626
})
2727

28-
await expect(promise).rejects.toThrowError('Request aborted')
28+
await expect(promise).rejects.toThrowError('This operation was aborted')
2929
}, 1000)

test/node-test/abort-controller.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
3636
abortController.abort()
3737

3838
client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
39-
p.ok(err instanceof errors.RequestAbortedError)
39+
p.ok(err instanceof errors.RequestAbortedError || err instanceof DOMException)
4040
})
4141
})
4242

@@ -73,7 +73,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
7373
})
7474

7575
client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
76-
p.ok(err instanceof errors.RequestAbortedError)
76+
p.ok(err instanceof errors.RequestAbortedError || err instanceof DOMException)
7777
})
7878

7979
abortController.abort()
@@ -98,7 +98,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
9898
t.after(client.destroy.bind(client))
9999

100100
client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
101-
p.ok(err instanceof errors.RequestAbortedError)
101+
p.ok(err instanceof errors.RequestAbortedError || err instanceof DOMException)
102102
})
103103
})
104104

@@ -122,7 +122,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
122122
t.after(client.destroy.bind(client))
123123

124124
client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
125-
p.ok(err instanceof errors.RequestAbortedError)
125+
p.ok(err instanceof errors.RequestAbortedError || err instanceof DOMException)
126126
})
127127
})
128128

@@ -149,7 +149,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
149149
abortController.abort()
150150
})
151151
response.body.on('error', err => {
152-
p.ok(err instanceof errors.RequestAbortedError)
152+
p.ok(err instanceof errors.RequestAbortedError || err instanceof DOMException)
153153
})
154154
})
155155
})
@@ -174,7 +174,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
174174
t.after(client.destroy.bind(client))
175175

176176
client.request({ path: '/', method: 'POST', body, signal: abortController.signal }, (err, response) => {
177-
p.ok(err instanceof errors.RequestAbortedError)
177+
p.ok(err instanceof errors.RequestAbortedError || err instanceof DOMException)
178178
})
179179
})
180180
await p.completed
@@ -204,7 +204,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
204204
t.after(client.destroy.bind(client))
205205

206206
client.request({ path: '/', method: 'POST', body, signal: abortController.signal }, (err, response) => {
207-
p.ok(err instanceof errors.RequestAbortedError)
207+
p.ok(err instanceof errors.RequestAbortedError || err instanceof DOMException)
208208
})
209209
})
210210
await p.completed
@@ -237,7 +237,7 @@ for (const { AbortControllerImpl, controllerName } of controllers) {
237237
abortController.abort()
238238
})
239239
response.body.on('error', err => {
240-
p.ok(err instanceof errors.RequestAbortedError)
240+
p.ok(err instanceof errors.RequestAbortedError || err instanceof DOMException)
241241
})
242242
})
243243
})

0 commit comments

Comments
 (0)