API Reference

isUndeclaredResponseError

Narrow a TanStack Query error to a response whose status the route does not declare.

Narrows an error from a query or mutation to UndeclaredResponseError. See the TanStack Query guide.

pnpm add @kizunajs/tanstack-query@beta
bun add @kizunajs/tanstack-query@beta
npm install @kizunajs/tanstack-query@beta
import { isUndeclaredResponseError } from '@kizunajs/tanstack-query';

Parameters

isUndeclaredResponseError(error: unknown): error is UndeclaredResponseError
ParameterTypeDescription
errorunknownThe error from a query or mutation

UndeclaredResponseError

PropertyTypeDescription
statusnumberThe status the server returned
bodyunknownThe parsed response body
headersRecord<string, string>The response headers

It extends Error, so retry, throwOnError, and error boundaries treat it as one.

When it is thrown

A route's declared statuses are the keys of its responses, plus 400 when the route has a body or query schema. Those arrive as data. Every other status throws.

getUser: k
    .route({
        method: 'GET',
        path: '/users/:id',
        responses: {
            200: UserSchema,
            404: ProblemDetailsSchema,
        },
    })
    .handler(/* ... */),

A 200 or 404 is data. A 500 throws.

Example

const { data, error } = useQuery(
    api.users.getUser.queryOptions({
        input: {
            params: {
                id: 'usr_abc123',
            },
        },
    })
);

if (data?.status === 404) {
    return <NotFound />;
}

if (error !== null && isUndeclaredResponseError(error)) {
    console.error(error.status, error.body);
}

See also

On this page