Prevent onError from useMutation from being called in mutationCache #5999
-
Hey everyone thanks for this fantastic library! I'm trying to understand whats the best way of error handleing globally in a react-query application What I wanted was a way to prevent the useMutation onError from running when the mutationCache already ran for a specific error. Example when there is a UNAUTHORIZED error after the createPostMutation is ran, I want to show the toast 'User not authorized' and I don't want to ran the onError of the useMutation const queryClient = new QueryClient({
mutationCache: new MutationCache({
onError: (error) => {
if (error instanceof CustomError) {
if (error.code === 'UNAUTHORIZED') {
toast({
title: 'User not authorized',
});
// If I retun a promise here the onError on useMutation will not run
// return new Promise();
}
},
}),
})
); // useCreatePost
const mutation = useMutation({
mutationFn: createPostMutation,
onSuccess: () => {
toast({
title: 'Success',
});
},
onError: (error) => {
if (error instanceof CustomError) {
if (error.code === 'POST_ALREADY_EXISTS') {
toast({
title: 'Post already exists',
});
return;
}
}
toast({
title: 'Other problems but not a unauthorized error',
});
},
}); One workaround that works is on the mutationCache return a promise.reject() this way the onError doesn't run his this the best way? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I don't think its meant to work that way but it's interesting that it does work 😅 |
Beta Was this translation helpful? Give feedback.
I don't think its meant to work that way but it's interesting that it does work 😅