diff --git a/.changeset/quiet-pugs-repeat.md b/.changeset/quiet-pugs-repeat.md new file mode 100644 index 00000000000..afe62028665 --- /dev/null +++ b/.changeset/quiet-pugs-repeat.md @@ -0,0 +1,5 @@ +--- +'@tanstack/react-query': patch +--- + +fix(react-query): throw falsy errors from `useMutation` to the error boundary diff --git a/packages/react-query/src/__tests__/useMutation.test.tsx b/packages/react-query/src/__tests__/useMutation.test.tsx index c23b75bf3a4..338cad67e40 100644 --- a/packages/react-query/src/__tests__/useMutation.test.tsx +++ b/packages/react-query/src/__tests__/useMutation.test.tsx @@ -1363,6 +1363,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(undefined) + }, + 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/react-query/src/useMutation.ts b/packages/react-query/src/useMutation.ts index 240c6f70d88..3f72b96cf96 100644 --- a/packages/react-query/src/useMutation.ts +++ b/packages/react-query/src/useMutation.ts @@ -63,7 +63,7 @@ export function useMutation< ) if ( - result.error && + result.isError && shouldThrowError(observer.options.throwOnError, [result.error]) ) { throw result.error