Skip to content

fix(query-core): add error management around this.setData(data) #7642

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

Closed
Closed
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
35 changes: 35 additions & 0 deletions packages/query-core/src/__tests__/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -943,4 +943,39 @@ describe('query', () => {
await sleep(60) // let it resolve
expect(spy).toHaveBeenCalledWith('1 - 2')
})

it('should call onError when setData throws', async () => {
const key = queryKey()

const queryFn = vi.fn<Array<unknown>, unknown>()

queryFn.mockImplementation(async () => {
await sleep(50)
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's get those numbers down:

Suggested change
await sleep(50)
await sleep(10)

const data: Array<{
id: number
name: string
link: null | { id: number; name: string; link: unknown }
}> = Array.from({ length: 5 })
.fill(null)
.map((_, index) => ({
id: index,
name: `name-${index}`,
link: null,
}))

if (data[0] && data[1]) {
data[0].link = data[1]
data[1].link = data[0]
}

return data
})

queryClient.prefetchQuery({ queryKey: key, queryFn })
const query = queryCache.find({ queryKey: key })!
await sleep(100)
Comment on lines +974 to +976
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
queryClient.prefetchQuery({ queryKey: key, queryFn })
const query = queryCache.find({ queryKey: key })!
await sleep(100)
await queryClient.prefetchQuery({ queryKey: key, queryFn })
const query = queryCache.find({ queryKey: key })!

expect(queryFn).toHaveBeenCalledTimes(1)
await query.fetch()
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't understand why we fetch twice: first with the prefetching, and then with query.fetch. Shouldn't await prefetchQuery(...) be enough to get the query into error state ?

expect(query.state.status).toBe('error')
})
})
8 changes: 6 additions & 2 deletions packages/query-core/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,12 @@ export class Query<
onError(new Error(`${this.queryHash} data is undefined`) as any)
return
}

this.setData(data)
try {
this.setData(data)
} catch (error) {
onError(error as TError)
return
}

// Notify cache callback
this.#cache.config.onSuccess?.(data, this as Query<any, any, any, any>)
Expand Down
Loading