Skip to content
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

support AbortSignal.reason #1775

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gomain
Copy link

@gomain gomain commented Sep 8, 2023

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.

Contrary to the specification, when AbortController#abort is called, with or without a reason, fetch does not throw the AbortSignal#reason. This is to allow more fine grained error handling. Had the reason was 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

  • Extend AbortError to have field reason. It's constructor takes this additional argument.
  • The AbortSignal#reason is given when constructing AbortError instance. Could possibly be undefined in environments/libraries/polyfills that do not support it.

Additional information

The actual value (even existence) of AbortSignal#reason is 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-fetch is for node, specifically, not the web. TBD.


  • I updated readme
  • I added unit test(s)

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`.
@@ -4,7 +4,9 @@ import {FetchBaseError} from './base.js';
* AbortError interface for cancelled requests
*/
export class AbortError extends FetchBaseError {
constructor(message, type = 'aborted') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a breaking change?

AbortError is exported publicly from the package...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AbortController abort reason Parameter
2 participants