Selector swallows errors #1540
Send to my agentDescribe the bug Query selector swallows any and all errors (syntax, type, ...) occurring in selector body
To Reproduce Declare query with selector fn
const query = useQuery(
"todos",
() => fetch("https://jsonplaceholder.typicode.com/posts").then((response) => response.json()),
{
select: (data) => {
throw new Error("ooops");
}
}
);
Expected behavior
query.isError === true
// and related flags to reflect same
or at least console log to appear, giving some heads up what to fix.
Code If applicable, add screenshots to help explain your problem.
Feel free to open a PR with a potential fix and we can discuss more there.
tannerlinsley added labelsenhancement
boschni(GitHub profile, opens in a new tab) closed this
I was having the same issue, a colleague lost a lot of time searching because of an error that occurred in a
selectfunction but was swallowed. The query returnedundefinedand they didn’t undertand why.I found a workaround using the following
QueryClientconfig:// log error only once per query const loggedQueryErrors = new Set<string>(); const globalQueryClient = new QueryClient({ defaultOptions: { throwOnError: (error, query) => { // You may filter errors here, e.g. network errors 400, etc. const logQueryKey = query.queryKey.join('/'); if (!loggedQueryErrors.has(logQueryKey)) { loggedQueryErrors.add(logQueryKey); console.error('Query error:', { queryKey: query.queryKey, error, ...(query.meta && { meta: query.meta }), }); } // don’t throw (default behavior) return false; }, } });
Sign in to join this conversation.
okay, I debugged this a bit:
when an error is thrown in the
selectfunction, it is caught inquery.ts:https://github.com/tannerlinsley/react-query/blob/361935a12cec6f36d0bd6ba12e84136c405047c5/src/core/query.ts#L406-L413
and the query is put into
errorstate. after that, we try to inform all the subscribers about this, which again triggers theselectfunction, but this time, the errors will be propagated to the "next" catch (since we are already in a catch), which is this:https://github.com/tannerlinsley/react-query/blob/361935a12cec6f36d0bd6ba12e84136c405047c5/src/core/queryObserver.ts#L269-L271
so the
noopswallows the error. not really sure what a good way around this would be. another try/catch around the dispatch in the catch block in query.ts would probably work, as the important part is the error propagation:https://github.com/tannerlinsley/react-query/blob/361935a12cec6f36d0bd6ba12e84136c405047c5/src/core/query.ts#L430-L431
I quickly tried this out, it seems to show the correct error in the console then:
I'd like to wait on some input from @tannerlinsley or @boschni though, since this would still swallow errors that happen in the error dispatch only.