Why does 400/401 trigger onSuccess? #1385
Replies: 4 comments 4 replies
-
This seems to be a common misconception with react-query expects you to return a failed promise, then it will go to error state. So you'd have to manually throw |
Beta Was this translation helpful? Give feedback.
-
For those looking for a way to handle 401 responses globally, this is what I use to clear out a session and redirect back to login: First, the
|
Beta Was this translation helpful? Give feedback.
-
I believe there are still some odd behaviors related to OPs post. I have a fetch that returns a 400. I wrote a fetch wrapper that when the 400 is encountered, it returns a Promise.reject. However, this seems to cause useMutation to throw a hard error which needs to be caught with a try/catch. Is that expected? Because I expect to be able to use the isSuccess/isError/error/etc properties to determine what happened rather than having to wrap all calls to useMutation in try/catch blocks. Yes, I have set throwOnError to false with no change in behaviour. |
Beta Was this translation helpful? Give feedback.
-
This works greatly. Thank you so much!!!
}, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am calling a POST API using react-query and fetch. When the fetch returns a 400 or 401 error I expect the onError method to fire as 400/401 are error codes. Instead onSuccess triggers - why is this? How do I make it so that 400 errors are handled by onError?
Example code;
Beta Was this translation helpful? Give feedback.
All reactions