Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

TanStack Query mutation says success when the POST failed

Errormutation.isError stays false after a 400 or 500 response

Short answer

fetch only rejects when the network fails. A 400 or 500 response still resolves, so TanStack Query sees a success and never sets isError. Check response.ok inside the mutation function and throw when it is false. Then isError, error and onError behave as expected.

  • Applies to TanStack Query 5, React Native 0.8x, Expo SDK 54
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log the status of the response inside the mutation function and see if it is not in the 200 range while the mutation still ends in success.

    mutationFn: async (body) => {
      const res = await fetch(url, { method: 'POST', body: JSON.stringify(body) });
      console.log(res.status, res.ok);
      return res.json();
    }
  2. 2Match what you see, then jump to its fix

    1. 1res.ok is false, yet the mutation status becomes successfetch resolves on HTTP error statuses and nothing throwsFix
    2. 2A try/catch around mutate() catches nothing, but the failure shows in the status fieldsYou expect mutate() to throw or return a promiseFix

What does the error look like?

  • mutation.isError stays false after a 400 or 500 response
  • onError is never called but onSuccess runs on a failed POST

Cause 1 of 2

fetch resolves on HTTP error statuses and nothing throws

1Check

The TanStack Query docs say fetch does not throw on unsuccessful responses by default, unlike axios, so you must throw yourself.

grep -rn "mutationFn" -A6 src app 2>/dev/null

2Fix

Check response.ok and throw an Error before you return the data.

useMutation({
  mutationFn: async (body) => {
    const res = await fetch(url, { method: 'POST', body: JSON.stringify(body) });
    if (!res.ok) {
      throw new Error('Network response was not ok');
    }
    return res.json();
  },
});

3Verify

Send a request that the server rejects; isError becomes true and error.message holds your text.

console.log(mutation.isError, mutation.error?.message);

Confirmed by tanstack.com

Did this fix it?

Cause 2 of 2

You expect mutate() to throw or return a promise

1Check

mutate does not return a promise you handle; you read the outcome from the status fields or from callbacks. Only mutateAsync returns a promise that throws on error.

grep -rn "mutate(\|mutateAsync(" src app 2>/dev/null

2Fix

Use onError on useMutation or in the mutate call, or switch to mutateAsync inside try/catch.

mutation.mutate(todo, {
  onError: (error) => console.log('Failed:', error.message),
});

try {
  await mutation.mutateAsync(todo);
} catch (error) {
  console.error(error);
}

3Verify

A failed request now reaches your onError or catch block.

# trigger a failing POST and watch the console

Confirmed by tanstack.com

Did this fix it?

How do I stop it coming back?

  • Write one small request helper that throws on !res.ok and use it in every mutation and query.
  • Read isError and error from the same useMutation result you called mutate on.
  • Call reset() to clear an old error before a retry when you show messages.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.