File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ import type { QueryClient } from './queryClient'
2222import { focusManager } from './focusManager'
2323import { Subscribable } from './subscribable'
2424import { getLogger } from './logger'
25+ import { isCancelledError } from './retryer'
2526
2627type 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
Original file line number Diff line number Diff 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 > [ ] = [ ]
You can’t perform that action at this time.
0 commit comments