query icon indicating copy to clipboard operation
query copied to clipboard

useInfiniteQuery: Explicit error handling for fetchNextPage/fetchPreviousPage

Open jasongerbes opened this issue 1 year ago β€’ 3 comments

Describe the bug

Using useInfiniteQuery, I would like to manually handle errors that occur during fetchNextPage or fetchPreviousPage, while displaying a generic message for errors that occur during initial load isLoadingError or refetch isRefetchError.

The fetchNextPage and fetchPreviousPage methods returns an isError property that can be used to show a message when a next/previous page fetch fails, however, there's no query-level property that allows you to differentiate between an initial load error, refetch error, and next/previous page fetch error.

Property Initial Load Refetch Page Next Page Previous Page
status βœ… βœ… βœ… βœ…
isError βœ… βœ… βœ… βœ…
isLoadingError βœ… ❌ ❌ ❌
isRefetchError ❌ βœ… βœ… βœ…

Your minimal, reproducible example

https://codesandbox.io/p/devbox/dank-tdd-plsmw6

Steps to reproduce

  1. Create a paginated query using useInfiniteQuery.
  2. Call fetchNextPage while simulating an API error.

Note: query.isRefetchError now equals true, making it impossible to differentiate refetch errors from next/previous page fetch errors.

Expected behavior

I would expect isRefetchError to only be true when a refetch error occurs (i.e. while reloading a previously loaded page).

Notably, while fetchNextPage is loading:

  • isRefetching: false
  • isFetchingNextPage: true.

This suggests that perhaps there should be more granular isFetchingNextPageError and isFetchingPreviousPageError properties to handle these specific scenarios.

Property Initial Load Refetch Page Next Page Previous Page
status βœ… βœ… βœ… βœ…
isError βœ… βœ… βœ… βœ…
isLoadingError βœ… ❌ ❌ ❌
isRefetchError ❌ βœ… ❌ ❌
isFetchNextPageError ❌ ❌ βœ… ❌
isFetchPreviousPageError ❌ ❌ ❌ βœ…

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Safari
  • Version: 17.4.1

Tanstack Query adapter

react-query

TanStack Query version

5.35.5

TypeScript version

No response

Additional context

No response

jasongerbes avatar May 11 '24 01:05 jasongerbes

I can see some inconsistencies here, especially with how isRefetching works. We've changed that to not be true for next / previous fetch here:

  • https://github.com/TanStack/query/pull/4465

So I agree that to be consistent, isRefetchError could also be false if a next/prev fetch failed. Currently, a refetch error is defined as an error that happened while you already have data in the cache. That's why it's true for next/prev fetches as well.

The info if the last fetch was a forward / backward fetch should be available on state.fetchMeta?.fetchMore?.direction, so the suggested change should be done somewhere here:

https://github.com/TanStack/query/blob/61f4d747883ce67cf4edcc65f9a7693fa2bc7b6a/packages/query-core/src/infiniteQueryObserver.ts#L152-L168

would you like to give it a shot at contributing ?

TkDodo avatar May 11 '24 13:05 TkDodo

Thanks for the quick response and background info. I’m happy to make a PR for this.

To make sure we’re on the same page, are you happy for me to make the following changes:

  • Update isRefetchError to be false when a next/previous page fetch fails.
  • Add isFetchNextPageError and isFetchPreviousPageError.
  • Update docs.

jasongerbes avatar May 11 '24 22:05 jasongerbes

@TkDodo, I had a bit of spare time today and created a PR for these suggested changes.

jasongerbes avatar May 12 '24 05:05 jasongerbes