TanStackT
TanStack13mo ago
1 reply
dry-scarlet

MutateAsync error handling (callback vs. try/catch/finally)

Is it more appropriate to handle the mutation lifecycle during mutateAsync using the callbacks on useMutation (onSuccess/onError/onSettled) or to use a try/catch/finally to do that?

Right now we're using mutateAsync during a form submission. The problem is it requires a try catch so if an error occurs it doesn't throw an unhandled exception. Some of the devs are catching the error and putting a //noop as a comment and handling errors via tha callbacks and others are handling the lifecycle within the try blocks instead.

Looking to see if anyone has a recommended pattern or if there's a preference so our team can maybe standardize it if possible.
Was this page helpful?