From 6b913f2c274d276589508f65b60bb83df0499638 Mon Sep 17 00:00:00 2001 From: Yogesh Kumar Date: Thu, 27 Aug 2026 11:42:55 +0530 Subject: [PATCH] fix(preact-query): propagate falsy errors from useMutation to the error boundary useMutation gated the error boundary throw on `result.error` being truthy, so a mutationFn that rejects with a falsy value such as `Promise.reject('')` leaves the mutation in the error state without the boundary ever seeing it. Gate on `result.isError` instead, matching useBaseQuery's getHasError and the same fix applied to useQueries in #11309. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_019MfJ49iHsRNpy4oibuXzrz --- .changeset/spotty-hounds-clap.md | 5 +++ .../src/__tests__/useMutation.test.tsx | 40 +++++++++++++++++++ packages/preact-query/src/useMutation.ts | 2 +- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 .changeset/spotty-hounds-clap.md diff --git a/.changeset/spotty-hounds-clap.md b/.changeset/spotty-hounds-clap.md new file mode 100644 index 00000000000..2ed4f6bcee3 --- /dev/null +++ b/.changeset/spotty-hounds-clap.md @@ -0,0 +1,5 @@ +--- +'@tanstack/preact-query': patch +--- + +fix(preact-query): throw falsy errors from `useMutation` to the error boundary diff --git a/packages/preact-query/src/__tests__/useMutation.test.tsx b/packages/preact-query/src/__tests__/useMutation.test.tsx index e24f79ddfba..8e8144f81f4 100644 --- a/packages/preact-query/src/__tests__/useMutation.test.tsx +++ b/packages/preact-query/src/__tests__/useMutation.test.tsx @@ -1364,6 +1364,46 @@ describe('useMutation', () => { consoleMock.mockRestore() }) + it('should be able to throw a falsy error when throwOnError is set to true', async () => { + const consoleMock = vi + .spyOn(console, 'error') + .mockImplementation(() => undefined) + function Page() { + const { mutate } = useMutation({ + mutationFn: () => { + return Promise.reject('') + }, + throwOnError: true, + }) + + return ( +
+ +
+ ) + } + + const { getByText, queryByText } = renderWithClient( + queryClient, + ( +
+ error boundary +
+ )} + > + +
, + ) + + fireEvent.click(getByText('mutate')) + + await vi.advanceTimersByTimeAsync(0) + expect(queryByText('error boundary')).not.toBeNull() + + consoleMock.mockRestore() + }) + it('should be able to throw an error when throwOnError is a function that returns true', async () => { const consoleMock = vi .spyOn(console, 'error') diff --git a/packages/preact-query/src/useMutation.ts b/packages/preact-query/src/useMutation.ts index 68a3759d93f..da5d0b79392 100644 --- a/packages/preact-query/src/useMutation.ts +++ b/packages/preact-query/src/useMutation.ts @@ -233,7 +233,7 @@ export function useMutation< ) if ( - result.error && + result.isError && shouldThrowError(observer.options.throwOnError, [result.error]) ) { throw result.error