probable-pink
probable-pink11mo ago

How to set global error and success notification?

I want to set the notification for error and success of API calls globally. Is there a way to implement that? Because in almost all of my error on API call I am duplicating the code with displaying the response error message from the API. For example my API will response error in 4xx as below:
{
status: false,
code: 400,
message: "Some error message that is related to validation",
details: {}
}
{
status: false,
code: 400,
message: "Some error message that is related to validation",
details: {}
}
Here I want my error notification to display the message from this response whenever my status lies between 4xx.
4 Replies
kapa.ai
kapa.ai11mo ago
Hi @dipbazz. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, you can set global notifications for API calls in Refine. Refine provides successNotification and errorNotification properties that you can use to customize the notifications. These properties are available in various hooks like useForm, useUpdateMany, useSelect, useAutocomplete, useList, useInfiniteList, and useMany. To display the error message from the API response, you can customize the errorNotification property. Here's an example of how you can do it:
useForm({
errorNotification: (data, values, resource) => {
return {
message: `Error: ${data.message}`,
description: "Error",
type: "error",
};
},
});
useForm({
errorNotification: (data, values, resource) => {
return {
message: `Error: ${data.message}`,
description: "Error",
type: "error",
};
},
});
In this example, data.message will display the error message from your API response. Please replace useForm with the appropriate hook based on your requirement. Please note that for these notifications to work, the NotificationProvider (https://refine.dev/docs/api-reference/core/providers/notification-provider/) is required. For more details, you can refer to the following documentation: - useForm (https://refine.dev/docs/api-reference/core/hooks/useForm/) - useUpdateMany (https://refine.dev/docs/api-reference/core/hooks/data/useUpdateMany/) - useSelect (https://refine.dev/docs/api-reference/core/hooks/useSelect/) - useAutocomplete (https://refine.dev/docs/api-reference/mui/hooks/useAutocomplete/) - useList (https://refine.dev/docs/api-reference/core/hooks/data/useList/) - useInfiniteList (https://refine.dev/docs/api-reference/core/hooks/data/useInfiniteList/) - useMany (https://refine.dev/docs/api-reference/core/hooks/data/useMany/) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai11mo ago
<@494653850632978472> kapa.ai is still learning and improving, please let us know how it did by reacting below
exotic-emerald
exotic-emerald11mo ago
Hello @dipbazz I think we have an issue for this one but never prioritized it. I'll check if we can do something in the coming weeks. https://github.com/refinedev/refine/issues/4006
GitHub
[FEAT] Single place to handle all notifications · Issue #4006 · ref...
Is your feature request related to a problem? Please describe. It might be useful to have a single place to configure all different notification messages for different resources, cause they might b...
probable-pink
probable-pink11mo ago
Hey @batuhanw looking forward to it with the solution.