-
Hello, Team react-query. Thank you for providing a good library. I'm using it well. However, I have some concerns while using it. Currently, I am handling errors for API requests globally in the QueryClient for query and mute requests. Below are some of the codes I use. new QueryClient({
queryCache: new QueryCache({
onError: (err) => errorHandler(err),
}),
mutationCache: new MutationCache({
onError: (error, _, _) => errorHandler(error)
}),
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
retry: false,
},
mutations: {
retry: false,
},
}, The problem I've experienced here is that I want to deal with errors customarily for specific requests. When sending query, mutation requests, I expected that adding onError to options would be an override, but it was not. Is there a way to handle the error I want instead of the global error in a particular request by passing it to the options in useMation? I need your help. Please help me. 😭 |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
Oh, I'm having the same problem as you. 😭 |
Beta Was this translation helpful? Give feedback.
-
So I would stick to the global handler on the queryCache / mutationCache. Those handlers do get the whole query / mutation passed as the second parameter, so you can decide to not handle something for a specific key. Also, you can use the
usage:
this is just an example. |
Beta Was this translation helpful? Give feedback.
-
In MutationCache onSuccess or onError callback, on 2nd parameter |
Beta Was this translation helpful? Give feedback.
-
For the sake of convenience, we have come to this approach to define a distinct hook for handling specific exceptions. export const DEFAULT_QUERY_CLIENT_CONFIG: Readonly<QueryClientConfig> = {
defaultOptions: { queries: {... } },
queryCache: new QueryCache({
onError: (error, query) => {
const meta = query?.meta ?? {}
if ('preventGlobalError' in meta && meta.preventGlobalError) {
reportError(error)
return
}
throw error
},
}),
}
export const useNonThrowingQuery = <
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
queryClient?: QueryClient,
): UseQueryResult<TData, TError> => {
return useQuery<TQueryFnData, TError, TData, TQueryKey>(
{
meta: { preventGlobalError: true },
...options,
},
queryClient,
)
}
|
Beta Was this translation helpful? Give feedback.
onError
added touseQuery
will overwrite the one passed todefaultOptions
. However, those error handlers will be executed for every observer (=useQuery instance).So I would stick to the global handler on the queryCache / mutationCache. Those handlers do get the whole query / mutation passed as the second parameter, so you can decide to not handle something for a specific key. Also, you can use the
meta
field on query/mutation to "tag" it in some way. This field is also available. For example, you could only show errors for queries that have a message passed: