Skip to content

Commit

Permalink
fix(useQuery): don't inform observers about CancelledErrors (#2409)
Browse files Browse the repository at this point in the history
  • Loading branch information
TkDodo authored Jun 26, 2021
1 parent 12e128b commit a778044
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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

1 comment on commit a778044

@vercel
Copy link

@vercel vercel bot commented on a778044 Jun 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.