-
Notifications
You must be signed in to change notification settings - Fork 541
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(fetch): use AbortError DOMException #1511
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1511 +/- ##
==========================================
+ Coverage 94.63% 94.65% +0.02%
==========================================
Files 50 50
Lines 4589 4588 -1
==========================================
Hits 4343 4343
+ Misses 246 245 -1
Continue to review full report at Codecov.
|
@@ -60,7 +60,26 @@ const subresource = [ | |||
'' | |||
] | |||
|
|||
/** @type {globalThis['DOMException']} */ | |||
const DOMException = globalThis.DOMException ?? (() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have a DomException in Node, no need for polyfill.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow missed this notification 😕.
DOMException is available in node globally from v17 and above, but fetch supports v16.5 and above. There's a comment detailing why this is needed a few lines below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can get it with:
try {
atob('~')
} catch (err) {
const DOMException = Object.getPrototypeOf(err).constructor
console.log(new DOMException('aaa'))
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in edc4f54
Should this support user-provided |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clearly marking that changes should br made here
No, the spec requires an AbortError DOMException and the error is verbatim from Firefox.
I'm unsure what you mean by this |
If you meant my question, it's completely not blocking. :)
Do we have any way to distinguish between arbitrary AFAIK there were12 common reasons in Node.js to use This feature is not widely implemented yet, and timeout was added in Firefox only in v100 so its current behaviour doesn't convince me yet. Footnotes |
Yes, you can use the const abortError = new DOMException('The operation was aborted.', 'AbortError')
abortError.name // 'AbortError'
abortError.code === DOMException.ABORT_ERR There doesn't seem to be any discussion in the fetch spec repo, this should probably be followed up there. However both Chrome & Firefox throw AbortError DOMExceptions.
In the issue mentioned, |
const controller = new AbortController();
setTimeout(() => controller.abort('custom reason'), 10);
fetch('https://github.com/nodejs/undici/pull/1511', {
signal: controller.signal
}).catch(err => {
console.error(err.name, err.code, err.message, err.cause);
});
fetch('https://github.com/nodejs/undici/pull/1511', {
signal: AbortSignal.timeout(10)
}).catch(err => {
console.error(err.name, err.code, err.message, err.cause);
}); In both cases I'm getting generic As an user, in (1), I need to have a way to retrieve Is it retrievable without having an explicit reference to // This acknowledges TimeoutError in Node.js and Firefox >=100; Chrome <=102 doesn't support it
AbortSignal.timeout(10).addEventListener('abort', function(err) { console.info(this.reason.name) }); // TimeoutError |
I'm getting the same results in the browser, this should be suggested at https://github.com/whatwg/fetch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I'll go ahead and fill an issue to WHATWG.
Some references from brief searching:
- There is a seemingly related opened PR for
fetch
specs: Add abort reason to abort fetch whatwg/fetch#1343 - PR which added
AbortSignal.timeout()
strongly emphasizes on ability to distinguish errors: Introduce AbortSignal.timeout() whatwg/dom#1032
My current impression (and suggestion to consider for undici implementation) on this is that having error.cause == signal.reason
would be the best solution. But I might be wrong. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
This commit uses the proper error when a request is aborted. A WPT actually checks for this behavior.
Honestly I didn't know if I should have put DOMException in constants or in util. I can change it if it matters or there's a preference.