Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-pugs-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/react-query': patch
---

fix(react-query): throw falsy errors from `useMutation` to the error boundary
40 changes: 40 additions & 0 deletions packages/react-query/src/__tests__/useMutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, undefined>({
mutationFn: () => {
return Promise.reject(undefined)
},
throwOnError: true,
})

return (
<div>
<button onClick={() => mutate()}>mutate</button>
</div>
)
}

const { getByText, queryByText } = renderWithClient(
queryClient,
<ErrorBoundary
fallbackRender={() => (
<div>
<span>error boundary</span>
</div>
)}
>
<Page />
</ErrorBoundary>,
)

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')
Expand Down
2 changes: 1 addition & 1 deletion packages/react-query/src/useMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function useMutation<
)

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