Errors
Hooks call the throwing form of an endpoint, so every failure reaches TanStack Query's error channel as the endpoint's EndpointError. This page shows the places that error turns up in React and how to branch on it there. What the kinds mean, and how errorSchema gives error bodies a type, is on Builder's Errors page.
In a hook result
error on a query or mutation result is already typed for its endpoint. Narrow on kind, then status, as you would on the result of .safe().
When the endpoint declares an errorSchema, the http kind carries the typed body. createUser as declared on Builder's Errors page describes its 409 reply:
A withResponse: true query or mutation changes only the success value. Failures are not wrapped.
In mutation callbacks
onError in the mutation settings receives the same typed error, and onSettled receives it or null.
mutateAsync rejects with the error. In the catch block it is unknown, so use the guards below, or prefer mutate and read error from the hook.
Error boundaries
useSuspense throws the error to the nearest error boundary, and so does use with TanStack's throwOnError option. A boundary receives unknown.
FallbackProps here is from react-error-boundary; any boundary works. To make "Try again" refetch, wrap the boundary in TanStack's QueryErrorResetBoundary.
When the error is unknown
Outside a hook result and a mutation callback, TypeScript no longer knows which endpoint failed. Two guards from @navios/builder get you back to a typed value:
isEndpointError(error)narrows toEndpointError, after whicherror.kindanderror.statuswork.isHttpError(error, status?)narrows to thehttpkind, meaning a status the endpoint declared inerrorSchema. A status without a schema ishttp-unknownand does not match; testerror.statusafterisEndpointErrorfor those, as the 404 above does.
To name an endpoint's error type, for a prop or a helper, use EndpointErrorType:
Retries
TanStack retries a failed query three times by default, and it does not know that some failures cannot succeed on a second attempt. Decide by kind once, on the QueryClient:
Without this, a 404 or a request-validation error, where nothing was sent at all, sits in the loading state through three pointless retries before it reaches error.
Reporting
For logging and telemetry, register onError on the builder. It sees every failed call, from hooks or not, once per attempt. See Reporting with onError on the Builder page.
Related: Forms maps validation errors to fields.