TanStackT
TanStack2y ago
3 replies
wispy-olive

Throw Error in Selector

I'm applying parser in my
select
function, bascially to ensure I have the correct data types from the server and transform the API response into an object that the type is defined by me.

I'm using zod to catch the errors and the parser function does throw error. Now, I would like to tell react query that there's something wrong when doing data transformation in
select
function. But it does nothing.

How to let react query knows that something wrong is happening?

select(data) {
      try {
        return data?.pages
          .map(page => page.map(inspection => parsers.inspection.detail(inspection)))
          .flatMap(data => data);
      } catch (error) {
        console.log(error);
        throw new Error('Invalid response from the server');
      }
}


I want the react query treat the error just like any API call error (rejected). Thus triggerin global onError function.

OR

do you think it's not the correct place to put the parser? Do I have to use it in my API function, the query function?
Was this page helpful?