T
TanStack3y ago
absent-sapphire

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);
});
},
});
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?
2 Replies
extended-salmon
extended-salmon3y ago
it defaults to undefined
absent-sapphire
absent-sapphireOP3y ago
Ah alright, thanks! I just saw that our codegen does only create queryKeys for useQuery but no mutationKeys...

Did you find this page helpful?