#queryClient.ensureQuery and throwing to ErrorBoundary

8 messages · Page 1 of 1 (latest)

pale orchid
#

Is there a way to make queries throw other things than Errors?

I'm using queryClient.ensureQuery with React Router's loader like this:

async function getFoo(foo: string) {
  const query = {
    queryKey: ['fooKey', foo],
    queryFn: async () => {
      const response = await fetch(`api.example.com/${foo}`);
      if (!response.ok) {
        throw response;
      }
      return await response.json();
    },
    useErrorBoundary: true,
  };

  return await queryClient.ensureQueryData(query);
}

export async function loader({params}) {
  const { fooId } = params;
  const foo = getFoo(fooId);
  return foo;
}

I expected to catch this in my React Router ErrorComponent, but it's not an Error object so it's ignored and ensureQuery returns.

solid warren
steel axle
#

not sure, maybe its a router thing that it doesn't catch things that aren't errors?
I would say its discouraged to throw things that are non-errors though. You could create your own error subclass and attach the response to it

#

in v5, we'll also default the error type to Error because everybody only throws errors and having unknown is just a huge burden for most users

pale orchid
pale orchid
solid warren
pale orchid