Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(query-core): make CancelledError extend Error #7843

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading