If I use the onError mutateoptions why cant I rethrow error and make notification provider catch it?
If I use the onError in mutateoptions why cant I rethrow error and make notification provider catch it?
onError mutation options doesn't trigger the notification provider as you might expect.onError in mutation options (like with , , etc.), you're intercepting the error at a point where the notification system has already processed it. The error handling flow in refine works like this:onError handleronError function runs, the notification has already been triggered. Rethrowing the error at this point won't cause the notification provider to catch it again because the error handling flow has already moved past the notification stage.useForm accept an property that lets you customize the error notification:useNotification hook directly with try-catch blocks, as suggested in a GitHub issue response:onError callback in , it overrides refine's default error handling behavior, including the automatic error notifications.onError in , use the property:onError for other reasons, you can manually trigger the notification:useUpdateuseUpdateuseUpdateuseUpdateuseDeleteuseDeleteuseDeleteerrorNotificationerrorNotificationerrorNotificationerrorNotificationonMutationErrormutationOptionsmutationOptions`mutationOptions` does not support `onSuccess` and `onError` props because they override the default `onSuccess` and `onError` functions.Error updating resource``
This issue is a known behavior in refine, as mentioned in [GitHub issue #5888](<https://github.com/refinedev/refine/issues/5888>), where it was noted that providing custom callbacks can override refine's default behaviors.
The recommended approach is to use the dedicated onError: (err: TError) => {
checkError(err);
queryOptions?.onError?.(err);
const notificationConfig =
typeof errorNotification === "function"
? errorNotification(err, { metaData, config }, resource)
: errorNotification;
handleNotification(notificationConfig, {
key: `${resource}-useList-notification`,
message: translate(
"common:notifications.error",
{ statusCode: err.statusCode },
`Error (status code: ${err.statusCode})`,
),
description: err.message,
type: "error",
});
},const { mutate } = useUpdate();
mutate({
errorNotification: (data, values, resource) => {
return {
message: `Something went wrong when getting ${data.id}`,
description: "Error",
type: "error",
};
},
});try {
// Your code that might throw an error
} catch (error) {
// Use useNotification to show custom error
notification.open({
type: "error",
message: "Custom error message",
description: error.message
});
}const { mutate } = useUpdate();
mutate({
resource: "products",
id: 1,
values: { /* your values */ },
errorNotification: (data, values, resource) => {
// Your custom error handling logic here
return {
message: `Something went wrong when updating ${resource}`,
description: "Error",
type: "error",
};
},
});