Skip to content

fix(react-query): propagate falsy errors from useMutation to the error boundary - #11311

Open
yogesh968 wants to merge 1 commit into
TanStack:mainfrom
yogesh968:fix/react-mutation-falsy-error
Open

fix(react-query): propagate falsy errors from useMutation to the error boundary#11311
yogesh968 wants to merge 1 commit into
TanStack:mainfrom
yogesh968:fix/react-mutation-falsy-error

Conversation

@yogesh968

@yogesh968 yogesh968 commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

useMutation decides whether to hand the error to the error boundary with:

if (
  result.error &&
  shouldThrowError(observer.options.throwOnError, [result.error])
) {
  throw result.error
}

The result.error && guard means a mutationFn that rejects with a falsy value never reaches the boundary. Promise.reject(), Promise.reject('') and Promise.reject(0) all leave the mutation in the error state, isError is true, but the component keeps rendering as if nothing went wrong and throwOnError is never consulted.

This is the same class of bug as #11305, which fixed it for useQueries and useSuspenseQueries. useQuery was already correct because getHasError in errorBoundaryUtils.ts gates on result.isError. useMutation was the one place left.

Solid and Angular already gate their equivalent checks on state.isError, so this brings React in line with them too.

The fix

Gate on result.isError instead of on the error value being truthy.

Test

packages/react-query/src/__tests__/useMutation.test.tsx gets a case where the mutationFn rejects with undefined and throwOnError: true is set. It expects the boundary fallback to render. On main it fails with expected null not to be null; with the change it passes.

Full @tanstack/react-query suite passes.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed useMutation so errors with falsy values, such as undefined, are correctly propagated to the error boundary when throwOnError is enabled.
  • Tests

    • Added coverage confirming rejected falsy values reach the error boundary.
  • Release

    • Included a patch release for @tanstack/react-query.

…r boundary

useMutation gated the error boundary throw on `result.error` being truthy. A
mutationFn that rejects with a falsy value, for example `Promise.reject()` or
`Promise.reject('')`, leaves the mutation in the error state but never reaches
the boundary, so the component keeps rendering as if nothing failed.

Gate on `result.isError` instead, matching what useQuery already does through
getHasError and what TanStack#11305 fixed for useQueries.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MfJ49iHsRNpy4oibuXzrz
@coderabbitai

coderabbitai Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: cd100c43-eb80-4c7f-9395-47f50b24dc22

📥 Commits

Reviewing files that changed from the base of the PR and between 714df67 and 5c16052.

📒 Files selected for processing (3)
  • .changeset/quiet-pugs-repeat.md
  • packages/react-query/src/__tests__/useMutation.test.tsx
  • packages/react-query/src/useMutation.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

useMutation now propagates falsy rejected errors, such as undefined, to the error boundary when throwOnError is enabled. A regression test covers this behavior, and a patch changeset records the fix.

Changes

Mutation error boundary handling

Layer / File(s) Summary
Throw falsy mutation errors
packages/react-query/src/useMutation.ts, packages/react-query/src/__tests__/useMutation.test.tsx, .changeset/quiet-pugs-repeat.md
useMutation uses result.isError when deciding whether to throw. The test verifies that a rejected undefined value renders the ErrorBoundary fallback. The changeset declares a patch release.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 5c160

This change makes falsy mutation rejections reach the error boundary as intended and adds focused regression coverage; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description clearly explains the bug, fix, affected behavior, and test coverage. However, it omits the required ## 🎯 Changes, ## ✅ Checklist, and ## 🚀 Release Impact sections and does not do… Add the required template sections. Complete the checklist items and identify the release impact, including confirmation that the changeset was generated.
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the primary change: propagating falsy useMutation errors to the error boundary.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description clearly explains the bug, fix, affected behavior, and test coverage. However, it omits the required ## 🎯 Changes, ## ✅ Checklist, and ## 🚀 Release Impact sections and does not document the checklist or release-impact selections.

Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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