API Reference

isUndeclaredResponseError

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

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

pnpm add @ts-kizuna/tanstack-query
bun add @ts-kizuna/tanstack-query
npm install @ts-kizuna/tanstack-query
import { isUndeclaredResponseError } from '@ts-kizuna/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: {
    method: 'GET',
    path: '/users/:id',
    responses: {
        200: UserSchema,
        404: ProblemDetailsSchema,
    },
},

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