Customizing error messages in global onError callback.
Very often I would like to curate the messages shown in a globally configured toast message that is dispatched by a global onError or onSuccess callback on a per mutation basis. For example, a confirmation message could say
Successfully created [name_of_thing]
where [name_of_thing]
comes from the mutation response. In the case of an error, [name_of_thing]
may need to come from local state if the network request fails. I can't use the meta
option because the data isn't static. I thought about formatting and returning a custom message in the local onSuccess and onError definition but I don't know if the global handler is able to grab this information. I may be overthinking this but I'm not sure how to approach this cleanly. Thanks.1 Reply
foreign-sapphire•2mo ago
make sure the error you return from your
mutationFn
has the correct error message, then just use error.message
globally. The transformation can happen inside the mutationFn