Skip to content

fix(hooks): propagate errors in useAsyncCallback to the React error boundary#612

Draft
Just-Insane wants to merge 5 commits intoSableClient:devfrom
Just-Insane:fix/async-callback-rejections
Draft

fix(hooks): propagate errors in useAsyncCallback to the React error boundary#612
Just-Insane wants to merge 5 commits intoSableClient:devfrom
Just-Insane:fix/async-callback-rejections

Conversation

@Just-Insane
Copy link
Copy Markdown
Contributor

Related to closed PR #548 (fix/media-error-handling), which was split into smaller focused PRs.

Description

useAsyncCallback was silently swallowing unhandled rejections from async callbacks. React's error boundary never received them, so errors were invisible to users and the UI could be left in a stuck loading state indefinitely. This fix wraps internal invocations so unhandled rejections are re-thrown on the next tick and surface to the nearest React error boundary.

Fixes #

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings

AI disclosure:

  • Partially AI assisted (clarify which code was AI assisted and briefly explain what it does).
  • Fully AI generated (explain what all the generated code does in moderate detail).

The error propagation wrapper in useAsyncCallback was partially AI assisted. I reviewed the boundary behaviour and verified errors surface correctly rather than being swallowed.

@Just-Insane Just-Insane requested review from 7w1 and hazre as code owners March 31, 2026 18:43
@Just-Insane Just-Insane marked this pull request as draft March 31, 2026 20:13
Wrap the inner callback with a no-op .catch() so fire-and-forget call
sites (e.g. loadSrc in useEffect) do not produce 'Uncaught (in promise)'
console warnings. The promise is still returned and re-thrown for callers
that await or chain, so intentional error handling is unaffected.
@Just-Insane Just-Insane force-pushed the fix/async-callback-rejections branch from 24fd8f7 to 918f2d7 Compare April 6, 2026 16:57
@Just-Insane Just-Insane marked this pull request as ready for review April 8, 2026 21:54
@Just-Insane Just-Insane deleted the fix/async-callback-rejections branch April 12, 2026 19:33
@Just-Insane Just-Insane restored the fix/async-callback-rejections branch April 12, 2026 19:41
@Just-Insane Just-Insane reopened this Apr 12, 2026
Copilot AI review requested due to automatic review settings April 15, 2026 15:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes error-handling behavior in the useAsyncCallback hook so callers that await/chain the returned promise can observe rejections (instead of needing to defensively swallow them), with accompanying test and changeset updates.

Changes:

  • Wrap useAsyncCallback’s returned function to attach a no-op rejection handler while still returning the original rejecting promise.
  • Update the hook test to assert the promise rejects when the async callback throws.
  • Add a changeset describing the intended behavior change.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/app/hooks/useAsyncCallback.ts Adds a wrapper around the async callback promise to attach a no-op .catch while returning the original promise.
src/app/hooks/useAsyncCallback.test.tsx Updates the error-path test to assert the returned promise rejects with the thrown error.
.changeset/async-callback-rejections.md Adds a patch changeset entry describing the fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +109 to 121

// Re-throw preserves rejection for callers that await/chain; the no-op .catch
// suppresses "Uncaught (in promise)" for fire-and-forget call sites (e.g.
// loadSrc() in a useEffect) without swallowing the error from intentional callers.
const callback = useCallback(
(...args: TArgs): Promise<TData> => {
const p = innerCallback(...args);
p.catch(() => {});
return p;
},
[innerCallback]
) as AsyncCallback<TArgs, TData>;

Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

The added no-op p.catch(() => {}) marks the rejection as handled, which prevents unhandledrejection from firing. In this repo Sentry is initialized with default browser integrations, so this will likely stop fire-and-forget failures from being captured/reported and can make production errors harder to detect. If the goal is to surface failures to the app’s error handling, consider either (a) removing the no-op catch and fixing call sites that truly need to ignore errors, or (b) reporting the error explicitly (e.g., via Sentry/logging) and/or rethrowing on a separate task so it still reaches global error handling.

Suggested change
// Re-throw preserves rejection for callers that await/chain; the no-op .catch
// suppresses "Uncaught (in promise)" for fire-and-forget call sites (e.g.
// loadSrc() in a useEffect) without swallowing the error from intentional callers.
const callback = useCallback(
(...args: TArgs): Promise<TData> => {
const p = innerCallback(...args);
p.catch(() => {});
return p;
},
[innerCallback]
) as AsyncCallback<TArgs, TData>;
const callback = innerCallback as AsyncCallback<TArgs, TData>;

Copilot uses AI. Check for mistakes.
Comment thread .changeset/async-callback-rejections.md Outdated
default: patch
---

Fix unhandled promise rejections in useAsyncCallback by propagating errors to the error boundary
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

This changeset entry claims errors are propagated “to the error boundary”, but the current implementation adds a no-op .catch that suppresses unhandled promise rejections (and React error boundaries don’t catch async rejections anyway). Please align the changeset wording with the actual behavior, or adjust the implementation to truly route unhandled async errors into the app’s chosen error-boundary/reporting mechanism.

Copilot uses AI. Check for mistakes.
…behaviour

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Just-Insane Just-Insane marked this pull request as draft April 17, 2026 11:55
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.

2 participants