Open
Conversation
In enviroments that implement it. When an `AbortSignal#reason` has a value, i.e. in nodejs >= v17.2.0, it is added on the returned `AbortError`. `AbortError#reason` would be `undefined` otherwise. Contary to the specification, when `AbortController#abort` is called, with or without a reason, `fetch` _does not_ throw the `AbortSignal#reason`.
LinusU
reviewed
Oct 2, 2023
| * AbortError interface for cancelled requests | ||
| */ | ||
| export class AbortError extends FetchBaseError { | ||
| constructor(message, type = 'aborted') { |
Member
There was a problem hiding this comment.
Is this a breaking change?
AbortError is exported publicly from the package...
Author
There was a problem hiding this comment.
Yeah, wasn't aware the constructor was actually exported. It just seemed odd that the type would be explicitly given, i.e. what else could it be.
We basically need to thread the reason into the thrown AbortError. To not break its constructor, the only sane option would be to extend it:
// no change to existing `AbortError`
class AbortWithReasonError extends AbortError {
constructor(message, reason) {
super(message);
this.reason = reason;
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In enviroments that implement it.
When an
AbortSignal#reasonhas a value, i.e. in nodejs >= v17.2.0, it is added on the returnedAbortError.AbortError#reasonwould beundefinedotherwise.Contrary to the specification, when
AbortController#abortis called, with or without a reason,fetchdoes not throw theAbortSignal#reason. This is to allow more fine grained error handling. Had thereasonwas thrown, the general information that an abort happened is lost and it becomes sole responsibility of the reason provider.Purpose
Allow detecting the reason fetch was aborted.
Changes
AbortErrorto have fieldreason. It's constructor takes this additional argument.AbortSignal#reasonis given when constructingAbortErrorinstance. Could possibly beundefinedin environments/libraries/polyfills that do not support it.Additional information
The actual value (even existence) of
AbortSignal#reasonis external to this library. We make no assumptions about it. We simply forward it.This goes against #1519. There is no inherent reason to follow the specifications to the dot.
node-fetchis for node, specifically, not the web. TBD.