Skip to content

useInfiniteQuery: Explicit error handling for fetchNextPage/fetchPreviousPage #7412

@jasongerbes

Description

@jasongerbes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions