Skip to content

Commit 35c086f

Browse files
authoredAug 2, 2024··
fix(query-core): make CancelledError extend Error (#7843)
* fix(query-core): make CancelledError extend Error * test: instanceof Error check * chore: delete left-over comment
1 parent 3814706 commit 35c086f

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed
 

‎packages/query-core/src/__tests__/query.test.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ describe('query', () => {
155155
const visibilityMock = mockVisibilityState('hidden')
156156

157157
let count = 0
158-
let result
158+
let result: unknown
159159

160160
const promise = queryClient.fetchQuery({
161161
queryKey: key,
@@ -183,8 +183,10 @@ describe('query', () => {
183183
// Check if the error is set to the cancelled error
184184
try {
185185
await promise
186+
expect.unreachable()
186187
} catch {
187188
expect(isCancelledError(result)).toBe(true)
189+
expect(result instanceof Error).toBe(true)
188190
} finally {
189191
// Reset visibilityState to original value
190192
visibilityMock.mockRestore()

‎packages/query-core/src/query.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -577,19 +577,19 @@ export class Query<
577577
}),
578578
}
579579
case 'error':
580-
const error = action.error as unknown
580+
const error = action.error
581581

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

586586
return {
587587
...state,
588-
error: error as TError,
588+
error,
589589
errorUpdateCount: state.errorUpdateCount + 1,
590590
errorUpdatedAt: Date.now(),
591591
fetchFailureCount: state.fetchFailureCount + 1,
592-
fetchFailureReason: error as TError,
592+
fetchFailureReason: error,
593593
fetchStatus: 'idle',
594594
status: 'error',
595595
}

‎packages/query-core/src/retryer.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ export function canFetch(networkMode: NetworkMode | undefined): boolean {
5454
: true
5555
}
5656

57-
export class CancelledError {
57+
export class CancelledError extends Error {
5858
revert?: boolean
5959
silent?: boolean
6060
constructor(options?: CancelOptions) {
61+
super('CancelledError')
6162
this.revert = options?.revert
6263
this.silent = options?.silent
6364
}

‎packages/react-query/src/useBaseQuery.ts

-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ export function useBaseQuery<
100100

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

0 commit comments

Comments
 (0)
Please sign in to comment.