NotFoundError, RedirectError, UnauthorizedError
The error classes behind the loader helpers, so an error screen can tell a missing page, a redirect and a refused guest apart.
import { NotFoundError, RedirectError, UnauthorizedError } from '@salla.sa/twilight-theme-engine/providers';In plain words
When a loader stops, what it throws is an object. These three classes give those objects a recognisable type, so an error screen can check error instanceof NotFoundError and show the right message.
Signature
class NotFoundError extends Error {
constructor(message?: string); // message default 'Not Found', name 'NotFoundError'
}
class UnauthorizedError extends Error {
constructor(message?: string); // message default 'Unauthorized', name 'UnauthorizedError'
}
class RedirectError extends Error {
constructor(path: string, replace?: boolean); // message 'Redirect to <path>', name 'RedirectError'
path: string;
replace: boolean; // default false
}Example
import {
NotFoundError,
RedirectError,
UnauthorizedError,
} from '@salla.sa/twilight-theme-engine/providers';
import { ErrorPage } from '@salla.sa/twilight-theme-engine/components/common';
export function PageError({ error }: { error: Error }) {
if (error instanceof RedirectError) return null;
if (error instanceof NotFoundError) return <ErrorPage code={404} message={error.message} />;
if (error instanceof UnauthorizedError) return <ErrorPage code={401} showAction={false} />;
return <ErrorPage code={500} message={error.message} />;
}
How it behaves
Who throws which, with
/tanstackloaded:unauthorized()andorUnauthorizedthrowUnauthorizedError, andorThrowthrowsNotFoundError.notFound()andredirect()throw TanStack Router's own objects instead;RedirectErrorcomes only from the unadaptedredirect().The router made by
createRouter()from/tanstackalready uses an error component like the example: nothing forRedirectError, a 404 page, a 401 page without the home button, 400 for API errors and 500 otherwise.
Related
Throw helpers for route loaders: stop and show the not-found page, send the visitor to another address, or refuse a guest.
orThrow, orUnauthorizedWrap an API call in a loader: orThrow turns any failure into a not-found error, orUnauthorized turns a 401 or 403 into the unauthorized flow.
Source and docs
- Engine source:
packages/theme-engine/src/providers/NavigationProvider.ts