Skip to content

Propagate request's response to be able to read status codes, and more #2317

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

Open
1 task done
konsalex opened this issue May 11, 2025 · 1 comment
Open
1 task done
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library

Comments

@konsalex
Copy link

openapi-fetch version

openapi-fetch

Description

Currently in the docs it says:

onError does not handle error responses with 4xx or 5xx HTTP status codes, since these are considered "successful" responses but with a bad status code. In these cases you need to check the response's status property or ok() method via the onResponse callback.

The issue with using react-query alongside openapi-ts + openapi-fetch that I see is that it seems impossible expect if I miss something to grab the response code.

The usage pattern would be:

 const { mutateAsync, isPending: isInviting } = useMutation("post", "/api/async-operation");


const handler = async () => {
  try {
     const res = await mutateAsync(...);
  } catch(error){
    // Here error code should be able to be read if it was Fetch error
   console.log(error) // Error just ends up being the body of the response and nothing else
  }
}

I believe from other issues #2257 #2231 and maybe more.

Reproduction

Expected result

Have the fetch's error response propagated if there is an error thrown.

Extra

@konsalex konsalex added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels May 11, 2025
@dannydi12
Copy link

I believe you're supposed to throw an error from within the fetch client so it can bubble up and let react-query react to it and pass it along into onError. Here's how I have mine set up:

export default function useApiClient() {
  const fetchClient = createFetchClient<paths>({
    baseUrl: import.meta.env.VITE_MERCHANT_API_URL,
    credentials: "include",
  });

  fetchClient.use({
    async onResponse({ response }) {
      if (!response.ok) {
        throw new FetchError("FetchError", response, response.json());
      }
      return response;
    },
  });

  const $api = createClient(fetchClient);

  return $api;
}
/// later on... in another file

const { mutateAsync, isPending, error } = $api.useMutation(
    "post",
    "/api/auth/login",
    {
      onSuccess: (data) => {
        navigate(ROUTES.DASHBOARD);
      },
      onError: (error) => {
          setFormError("Invalid email or password");
      },
    }
  );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

No branches or pull requests

2 participants