Can I log errors from queries & mutations without throwing them? (struggling with error handling)
I would like to log errors that occur within queries/mutations to the developer console. I know I can set
throwOnError to true to have the errors thrown, but I've already built my app to use the error states from my queries/mutations and would rather not have to rebuild all of that. I can set a default onError function for mutations, but that doesn't exist for queries, and even then I'd need to re-implement that for any mutation that needed its own onError handler. I also tried to get fancy(/hacky) and log the error in a throwOnError function, but it looks like throwOnError can get rerun even when the error hasn't changed which causes the error to log multiple times. It also looks like the behaviour from throwOnError is for errors to be thrown from the query provider rather than the location of the hook. On top of that, mutateAsync seems to do its own thing with respect to error handling. This seems to result in a lot of different scenarios to consider when deciding how to log errors in a consistent fashion. This may be a naive question, but is the best way to do this just to line every query/mutation/select function in a try/catch that logs any errors and then re-throws them to the query/mutation? Thanks!2 Replies
blank-aquamarine•11mo ago
Why not just add
onError for mutationCache and queryCache on your queryClient: https://tkdodo.eu/blog/react-query-error-handling#the-global-callbacksReact Query Error Handling
After covering the sunshine cases of data fetching, it's time to look at situations where things don't go as planned and "Something went wrong..."
solid-orangeOP•11mo ago
That's exactly what I wanted - thank you!