Skip to content
Source
Sign inSign up
TanStack/query

Selector swallows errors #1540

Send to my agent
ClosedAndreyco(GitHub profile, opens in a new tab) opened this issue 4 comments

Describe 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.

  1. okay, I debugged this a bit:

    when an error is thrown in the select function, it is caught in query.ts:

    https://github.com/tannerlinsley/react-query/blob/361935a12cec6f36d0bd6ba12e84136c405047c5/src/core/query.ts#L406-L413

    and the query is put into error state. after that, we try to inform all the subscribers about this, which again triggers the select function, 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 noop swallows 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

    if (!(isCancelledError(error) && error.silent)) {
    + try {
         this.dispatch({
           type: 'error',
           error,
         })
    + } catch { }
    }

    I quickly tried this out, it seems to show the correct error in the console then:

    Screenshot 2021-01-08 at 15 29 29

    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.

  2. Feel free to open a PR with a potential fix and we can discuss more there.

  3. tannerlinsley added labelsenhancement

  4. ghostcommented

    hope to add an option to throw errors, otherwise it will be very troublesome, we don't know why when app just broken

  5. I was having the same issue, a colleague lost a lot of time searching because of an error that occurred in a select function but was swallowed. The query returned undefined and they didn’t undertand why.

    I found a workaround using the following QueryClient config:

    // 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.