TanStackT
TanStack3y ago
2 replies
faint-white

How to get mutationKey in onError?

Hi, in my global ReactQueryClientProvider I try to have global error handling for mutations by setting the MutationCache.
  const mutationCache = new MutationCache({
    onError: (error, _variables, _context, mutation) => {
      
      withScope((scope) => {
        
        // ignore issue if issue will be handled by local error handler
        //if (mutation.options.onError) return;

        // group issues for sentry report
        const fingerprint = mutation.options.mutationKey;

        scope.setContext('mutation', {
          mutationId: mutation.mutationId,
          variables: mutation.state.variables,
        });

        if (mutation.options.mutationKey) {
          scope.setFingerprint(Array.from(mutation.options.mutationKey) as string[]);
        }

        // sentry error logging
        captureException(error);
      });
    },
  });

but somehow I do not get any mutationKey from mutation. Do I have to set mutationKey manually when triggering mutation or is there a default mutationKey?
Was this page helpful?