Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/core/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type { QueryClient } from './queryClient'
import { focusManager } from './focusManager'
import { Subscribable } from './subscribable'
import { getLogger } from './logger'
import { isCancelledError } from './retryer'

type QueryObserverListener<TData, TError> = (
result: QueryObserverResult<TData, TError>
Expand Down Expand Up @@ -660,7 +661,7 @@ export class QueryObserver<

if (action.type === 'success') {
notifyOptions.onSuccess = true
} else if (action.type === 'error') {
} else if (action.type === 'error' && !isCancelledError(action.error)) {
notifyOptions.onError = true
}

Expand Down
27 changes: 27 additions & 0 deletions src/react/tests/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,33 @@ describe('useQuery', () => {
consoleMock.mockRestore()
})

it('should not call onError when receiving a CancelledError', async () => {
const key = queryKey()
const onError = jest.fn()
const consoleMock = mockConsoleError()

function Page() {
useQuery<unknown>(
key,
async () => {
await sleep(10)
return 23
},
{
onError,
}
)
return null
}

renderWithClient(queryClient, <Page />)

await sleep(5)
await queryClient.cancelQueries(key)
expect(onError).not.toHaveBeenCalled()
consoleMock.mockRestore()
})

it('should call onSettled after a query has been fetched', async () => {
const key = queryKey()
const states: UseQueryResult<string>[] = []
Expand Down