Skip to content

Commit

Permalink
Reject requests with AbortSignal reason
Browse files Browse the repository at this point in the history
  • Loading branch information
steveluscher authored and JakeChampion committed Apr 8, 2024
1 parent ba5cf1e commit 8462d79
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 7 deletions.
4 changes: 2 additions & 2 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ export function fetch(input, init) {
var request = new Request(input, init)

if (request.signal && request.signal.aborted) {
return reject(new DOMException('Aborted', 'AbortError'))
return reject(request.signal.reason);
}

var xhr = new XMLHttpRequest()
Expand Down Expand Up @@ -570,7 +570,7 @@ export function fetch(input, init) {

xhr.onabort = function() {
setTimeout(function() {
reject(new DOMException('Aborted', 'AbortError'))
reject(request.signal ? request.signal.reason : new DOMException('Aborted', 'AbortError'))
}, 0)
}

Expand Down
110 changes: 105 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ exercise.forEach(function(exerciseMode) {
assert.deepEqual(controller.signal, request.signal);
})

test('initially aborted signal', function () {
test('initially aborted signal without reason', function () {
var controller = new AbortController()
controller.abort()

Expand All @@ -1221,8 +1221,24 @@ exercise.forEach(function(exerciseMode) {
}
)
})

test('initially aborted signal with reason', function () {
var controller = new AbortController()
controller.abort('Something bad happened')

test('initially aborted signal within Request', function() {
return fetch('/request', {
signal: controller.signal
}).then(
function() {
assert.ok(false)
},
function(error) {
assert.equal(error, 'Something bad happened')
}
)
})

test('initially aborted signal without reason within Request', function() {
var controller = new AbortController()
controller.abort()

Expand All @@ -1238,7 +1254,23 @@ exercise.forEach(function(exerciseMode) {
)
})

test('mid-request', function() {
test('initially aborted signal with reason within Request', function() {
var controller = new AbortController()
controller.abort('Something bad happened')

var request = new Request('/request', {signal: controller.signal})

return fetch(request).then(
function() {
assert.ok(false)
},
function(error) {
assert.equal(error, 'Something bad happened')
}
)
})

test('mid-request without reason', function() {
var controller = new AbortController()

setTimeout(function() {
Expand All @@ -1256,8 +1288,27 @@ exercise.forEach(function(exerciseMode) {
}
)
})

test('mid-request with reason', function() {
var controller = new AbortController()

setTimeout(function() {
controller.abort('Something bad happened')
}, 30)

return fetch('/slow?_=' + new Date().getTime(), {
signal: controller.signal
}).then(
function() {
assert.ok(false)
},
function(error) {
assert.equal(error, 'Something bad happened')
}
)
})

test('mid-request within Request', function() {
test('mid-request without reason within Request', function() {
var controller = new AbortController()
var request = new Request('/slow?_=' + new Date().getTime(), {signal: controller.signal})

Expand All @@ -1275,7 +1326,25 @@ exercise.forEach(function(exerciseMode) {
)
})

test('abort multiple with same signal', function() {
test('mid-request with reason within Request', function() {
var controller = new AbortController()
var request = new Request('/slow?_=' + new Date().getTime(), {signal: controller.signal})

setTimeout(function() {
controller.abort('Something bad happened')
}, 30)

return fetch(request).then(
function() {
assert.ok(false)
},
function(error) {
assert.equal(error, 'Something bad happened')
}
)
})

test('abort multiple without reason with same signal', function() {
var controller = new AbortController()

setTimeout(function() {
Expand Down Expand Up @@ -1305,6 +1374,37 @@ exercise.forEach(function(exerciseMode) {
)
])
})

test('abort multiple with reason with same signal', function() {
var controller = new AbortController()

setTimeout(function() {
controller.abort('Something bad happened')
}, 30)

return Promise.all([
fetch('/slow?_=' + new Date().getTime(), {
signal: controller.signal
}).then(
function() {
assert.ok(false)
},
function(error) {
assert.equal(error, 'Something bad happened')
}
),
fetch('/slow?_=' + new Date().getTime(), {
signal: controller.signal
}).then(
function() {
assert.ok(false)
},
function(error) {
assert.equal(error, 'Something bad happened')
}
)
])
})
})

suite('response', function() {
Expand Down

0 comments on commit 8462d79

Please sign in to comment.