Skip to content

Commit

Permalink
fix(query-core): make CancelledError extend Error (TanStack#7843)
Browse files Browse the repository at this point in the history
* fix(query-core): make CancelledError extend Error

* test: instanceof Error check

* chore: delete left-over comment
  • Loading branch information
TkDodo authored Aug 2, 2024
1 parent 3814706 commit 35c086f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/query-core/src/__tests__/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe('query', () => {
const visibilityMock = mockVisibilityState('hidden')

let count = 0
let result
let result: unknown

const promise = queryClient.fetchQuery({
queryKey: key,
Expand Down Expand Up @@ -183,8 +183,10 @@ describe('query', () => {
// Check if the error is set to the cancelled error
try {
await promise
expect.unreachable()
} catch {
expect(isCancelledError(result)).toBe(true)
expect(result instanceof Error).toBe(true)
} finally {
// Reset visibilityState to original value
visibilityMock.mockRestore()
Expand Down
6 changes: 3 additions & 3 deletions packages/query-core/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,19 +577,19 @@ export class Query<
}),
}
case 'error':
const error = action.error as unknown
const error = action.error

if (isCancelledError(error) && error.revert && this.#revertState) {
return { ...this.#revertState, fetchStatus: 'idle' }
}

return {
...state,
error: error as TError,
error,
errorUpdateCount: state.errorUpdateCount + 1,
errorUpdatedAt: Date.now(),
fetchFailureCount: state.fetchFailureCount + 1,
fetchFailureReason: error as TError,
fetchFailureReason: error,
fetchStatus: 'idle',
status: 'error',
}
Expand Down
3 changes: 2 additions & 1 deletion packages/query-core/src/retryer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ export function canFetch(networkMode: NetworkMode | undefined): boolean {
: true
}

export class CancelledError {
export class CancelledError extends Error {
revert?: boolean
silent?: boolean
constructor(options?: CancelOptions) {
super('CancelledError')
this.revert = options?.revert
this.silent = options?.silent
}
Expand Down
3 changes: 0 additions & 3 deletions packages/react-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ export function useBaseQuery<

// Handle suspense
if (shouldSuspend(defaultedOptions, result)) {
// Do the same thing as the effect right above because the effect won't run
// when we suspend but also, the component won't re-mount so our observer would
// be out of date.
throw fetchOptimistic(defaultedOptions, observer, errorResetBoundary)
}

Expand Down

0 comments on commit 35c086f

Please sign in to comment.