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