Skip to content

feat(client): propagate AbortSignal reason to APIUserAbortError #1611

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

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

Conversation

christian-bromann
Copy link

@christian-bromann christian-bromann commented Aug 11, 2025

  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

This PR enhances the error handling for aborted requests by propagating the abort reason from the AbortSignal to the APIUserAbortError.

What changed:

  • Modified APIUserAbortError instantiation to include the abort signal's reason as the error message
  • Updated two locations in client.ts where APIUserAbortError is thrown to pass the abort reason
  • Added comprehensive tests to verify:
    • Custom abort reasons are properly propagated to the error
    • Pre-aborted signals prevent requests from being made

Why this is useful:

  • Developers can now provide custom abort reasons (e.g., controller.abort('user cancelled')) and receive them in error handlers
  • Improves debugging by providing context about why a request was aborted
  • Maintains backward compatibility while adding useful functionality

Additional context & links

This change addresses the common use case where applications need to distinguish between different types of request cancellations (user-initiated vs timeout vs navigation away, etc.). By propagating the abort reason, error handlers can make more informed decisions about how to respond to aborted requests.

Example usage:

const controller = new AbortController();
// Abort with a reason
controller.abort('User navigated away');

try {
  await client.get('/foo', { signal: controller.signal });
} catch (error) {
  if (error instanceof APIUserAbortError) {
    console.log(error.message); // "User navigated away"
  }
}

- Pass abort signal reason to APIUserAbortError constructor
- Update error handling in fetchWithTimeout to include abort reason
- Add tests to verify abort reason is properly propagated
- Add test to ensure no request is made when signal is pre-aborted

This allows consumers to provide and receive custom abort reasons when
cancelling requests, improving debugging and error handling capabilities.
@christian-bromann christian-bromann requested a review from a team as a code owner August 11, 2025 21:02
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.

1 participant