T
TanStack•10mo ago
conscious-sapphire

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); } };
No description
3 Replies
conscious-sapphire
conscious-sapphireOP•10mo ago
the try catch not able to catch the error thrown by mutation.mutationAsync
flat-fuchsia
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
helpful-purple•10mo ago
would you get the same thing with:
async function myFn() {
throw new Error("djfljafjoijioejfioajoisdjfoijdaiojfo")
}

const res = await myFn(val)
async function myFn() {
throw new Error("djfljafjoijioejfioajoisdjfoijdaiojfo")
}

const res = await myFn(val)
? because this is pretty much what we are doing 🙂

Did you find this page helpful?