mutateAsync throwing error in nextjs
const mutation = useMutation({
mutationFn: () => {
throw new Error("djfljafjoijioejfioajoisdjfoijdaiojfo");
},
onSuccess: () => {
// console.log("succk");
},
onError: (err) => {
// console.log("err");
},
});
const onSubmit = async (val) => {
try {
const res = await mutation.mutateAsync(val);
console.log("resssss: ", res);
} catch (error) {
console.log("errrrrr: ", error);
}
};

3 Replies
conscious-sapphireOP•10mo ago
the try catch not able to catch the error thrown by mutation.mutationAsync
flat-fuchsia•10mo ago
mutateFn should return a promise either resolved or rejected to be caught by a try-catch. You do not return anything, instead, you
throw Error
since this mutateFn is not wrapped with a try-catch and is called by a third-party code. The throw
is happening outside of the context of try-catch.helpful-purple•10mo ago
would you get the same thing with:
?
because this is pretty much what we are doing 🙂