Skip to content

Bug Report: Response["error"] does not type all thrown errors (middleware/fetch errors missing) #2372

Open
@Mathieu-COSYNS

Description

@Mathieu-COSYNS

openapi-react-query version

0.5.0

Description

The current implementation of error typing in useQuery, useMutation, and similar hooks only types the Response["error"] field returned by openapi-fetch. However, this does not account for:

  • fetch-level errors (e.g. network errors)
  • Errors thrown by custom middlewares
  • Other runtime exceptions

As a result, the type assigned to the error field in React Query hooks is incomplete and potentially misleading, as consumers might assume that all thrown errors conform to Response["error"]. This typing issue can lead to subtle runtime bugs and TypeScript's false sense of safety. The type should be widened to reflect real-world thrown values.

Reproduction

Consider the following usage:

const { error } = useQueryClient.useQuery("get", "/some-endpoint");

if (error) {
  // TS assumes `error` is `Response["error"]`, e.g. an API error
  console.error(error);
}

But error could be any of the following:

  • A error thrown by the fetch() call (e.g. network failure)
  • A custom error thrown by a user-defined middleware
  • ...

Yet, the generic types currently look like this:

UseQueryResult<TData, Response["error"]>

And Response["error"] is tied to what openapi-fetch returns — not all the above.

Expected result

React Query's error field should allow for a broader type to reflect all possible thrown errors.

We suggest modifying the generics to include unknown or Error union:

UseQueryResult<TData, Response["error"] | Error>

Or even more accurately:

UseQueryResult<TData, unknown>

This would better reflect the reality that many non-API errors can be thrown during the request lifecycle.

Proposed Fix

Update all references to the error field in the generics to broaden its type. For example:

UseQueryResult<..., unknown>
UseMutationResult<..., unknown, ...>

Alternatively, provide a customization hook or override so downstream users can define their own union error type.

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-react-queryRelevant to openapi-react-query

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions