TanStackT
TanStack3y ago
8 replies
scornful-crimson

queryClient.ensureQuery and throwing to ErrorBoundary

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.
Was this page helpful?