onError vs try catch vs then catch

Hi I am using following mutation:

const [mutate, {loading}] = useMyMutation({
    onError: (error) => {
      console.log('error', error);
    },
  });

And then calling it like

mutate()
    .then((data) => {
      console.log('ALOO ~ mutate ~ data', data);
    })
    .catch((error) => {
      console.log('ALOO ~ mutate ~ error', error);
    });

But if I remove the “catch” from it the “then” code is being run even if there is an error.
Is this intentional?

Yeah, I don’t think this is very well documented but if your useMutation hook has an onError property, then the mutate function will never throw an error. If you remove the onError from useMutation then the mutate function can throw errors.

2 Likes

Yeah I didn’t find any thing on the documentation about this behaviour.
Do you know any way to change it so that at least the then function don’t run?
Thanks

Only way I know of is to remove onError from the useMutation hook