T
TanStack3y ago
conscious-sapphire

Are the errors in onSuccess callback are catch ?

Hello everyone, I have a pretty simple question but i can't find the answer. Imagine i have something like that:
useMutation({
mutationFn: (newComment) =>
axios.post(`/posts/${id}/comments`, newComment),
onSuccess: () => {
// ✅ refetch the comments list for our blog post
queryClient.invalidateQueries({ queryKey: ['posts', id, 'comments'] })
},
})
useMutation({
mutationFn: (newComment) =>
axios.post(`/posts/${id}/comments`, newComment),
onSuccess: () => {
// ✅ refetch the comments list for our blog post
queryClient.invalidateQueries({ queryKey: ['posts', id, 'comments'] })
},
})
invalidate could throw an error if for whatever reason it failed no ? And in this case, is the error catch or should wrap the invalidateQueries inside a try/catch ? Thanks for your help 🙂
3 Replies
dependent-tan
dependent-tan3y ago
invalidateQueries doesn't throw errors, so no try/catch needed
conscious-sapphire
conscious-sapphireOP3y ago
oh ok x) i was not sure about that, thanks for your response i was thinking that beacause in type definition invalidateQueries return Promise<void> so i assumed it could throw ok i see, it only throw if i pass throwOnError: true
dependent-tan
dependent-tan3y ago
Yeah it does .catch(noop) per default

Did you find this page helpful?