TanStack Query mutation says success when the POST failed
mutation.isError stays false after a 400 or 500 responseShort 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.
Fix it fast
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(); }2Match what you see, then jump to its fix
What does the error look like?
mutation.isError stays false after a 400 or 500 responseonError 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/null2Fix
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
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/null2Fix
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 consoleConfirmed by tanstack.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
onSuccess and onError on useQuery do nothing after upgrading TanStack Query
onError callback not working after upgrading to React Query v5
TanStack Query v5 removed onSuccess, onError and onSettled from queries, so options passed to useQuery are ignored.
2 causes, each with a fixMobile apps
Android build stops at processDebugManifest in a React Native app
Manifest merger failed
processDebugManifest is the step that merges your AndroidManifest.xml with the manifests of every library.
2 causes, each with a fixMobile apps
Running pod on a React Native project fails with can't find gem cocoapods
can't find gem cocoapods (>= 0.a) with executable pod
The pod command exists, but the Ruby that is running it has no CocoaPods gem.
3 causes, each with a fixMobile apps
React Native Android build cannot find the foojay-resolver-convention plugin
Plugin [id: 'org.gradle.toolchains.foojay-resolver-convention'] was not found in any of the following sources:
The foojay plugin is a settings plugin that Gradle downloads from the Plugin Portal.
3 causes, each with a fixMobile apps
Kotlin and Java compile to different JVM targets in a React Native Android build
'compileJava' task (current target is 1.8) and 'compileKotlin' task (current target is 11)
Gradle refuses to build when the Java task and the Kotlin task target different JVM versions.
3 causes, each with a fixMobile apps
Gradle cannot run node in a React Native Android build
Process 'command 'node'' finished with non-zero exit value 1
The React Native Gradle plugin starts node for bundling.
3 causes, each with a fix