Skip to content

Commit a778044

Browse files
authored
fix(useQuery): don't inform observers about CancelledErrors (#2409)
1 parent 12e128b commit a778044

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/core/queryObserver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { QueryClient } from './queryClient'
2222
import { focusManager } from './focusManager'
2323
import { Subscribable } from './subscribable'
2424
import { getLogger } from './logger'
25+
import { isCancelledError } from './retryer'
2526

2627
type QueryObserverListener<TData, TError> = (
2728
result: QueryObserverResult<TData, TError>
@@ -660,7 +661,7 @@ export class QueryObserver<
660661

661662
if (action.type === 'success') {
662663
notifyOptions.onSuccess = true
663-
} else if (action.type === 'error') {
664+
} else if (action.type === 'error' && !isCancelledError(action.error)) {
664665
notifyOptions.onError = true
665666
}
666667

src/react/tests/useQuery.test.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,33 @@ describe('useQuery', () => {
473473
consoleMock.mockRestore()
474474
})
475475

476+
it('should not call onError when receiving a CancelledError', async () => {
477+
const key = queryKey()
478+
const onError = jest.fn()
479+
const consoleMock = mockConsoleError()
480+
481+
function Page() {
482+
useQuery<unknown>(
483+
key,
484+
async () => {
485+
await sleep(10)
486+
return 23
487+
},
488+
{
489+
onError,
490+
}
491+
)
492+
return null
493+
}
494+
495+
renderWithClient(queryClient, <Page />)
496+
497+
await sleep(5)
498+
await queryClient.cancelQueries(key)
499+
expect(onError).not.toHaveBeenCalled()
500+
consoleMock.mockRestore()
501+
})
502+
476503
it('should call onSettled after a query has been fetched', async () => {
477504
const key = queryKey()
478505
const states: UseQueryResult<string>[] = []

0 commit comments

Comments
 (0)