ErrorPage
A full error screen with a code, a title, a message and a button home; the default router error screen uses it.
import { ErrorPage } from '@salla.sa/twilight-theme-engine/components/common';In plain words
When a page cannot be shown (it does not exist, or something broke), the shopper should see a clear message and a way back, not a blank screen. ErrorPage is that screen: a big code such as 404, a title, a sentence, and a "Back home" button, in the page's language.
Signature
const ErrorPage: LazyExoticComponent<(props: ErrorPageProps) => JSX.Element>
// ErrorPageProps is not exported
interface ErrorPageProps {
code?: string | number; // the large number at the top
title?: string; // default t('common.errors.<code>')
message?: string; // default t('common.errors.error_occurred')
actionText?: string; // default t('common.elements.back_home')
actionHref?: string; // default '/'
showAction?: boolean; // default true
LinkComponent?: ComponentType<{
to: string; className?: string; style?: CSSProperties; children: ReactNode;
}>; // default: a plain <a href>
children?: ReactNode; // between the message and the button
}Try it live
import { ErrorPage, Link } from '@salla.sa/twilight-theme-engine/components/common';
export function NotFound() {
return <ErrorPage code={404} LinkComponent={Link} />;
}
Example
import { ErrorPage, Link } from '@salla.sa/twilight-theme-engine/components/common';
import { useTranslation } from '@salla.sa/twilight-theme-engine/i18n';
export function ProductGone() {
const { t } = useTranslation();
return (
<ErrorPage
code={404}
message={t('pages.product.gone', 'This product is no longer available.')}
actionText={t('pages.product.see_offers', 'See the offers')}
actionHref="/offers"
LinkComponent={Link}
/>
);
}
How it behaves
Layout: a
containerwith the code in an<h1>, the title in an<h2>, the message in a<p>, yourchildren, then the button, colored with the CSS variables--color-primaryand--color-primary-reverse.The router made by
createRouter()uses it for its default error screen: 404 forNotFoundError, 401 without the button forUnauthorizedError, 400 for API errors and 500 for anything else, each with the error's message.It is lazy (code-split).
Gotchas
The default texts are translation keys, and a missing translation returns the key itself (i18next's behaviour), so the
?? 'Error'fallbacks in the source never apply. Salla's storefront translations covercommon.errors.404and no other code:code={500}shows the heading "common.errors.500", and a page with neithercodenortitleshows "common.errors.undefined". Pass atitlefor any code but 404.Without
LinkComponentthe button is a plain<a href="/">: clicking it reloads the whole app, andactionHrefgets no locale. PassLinkComponent={Link}from the same import.
Related
The error classes behind the loader helpers, so an error screen can tell a missing page, a redirect and a refused guest apart.
NoContentThe empty-state block: an icon, a message and an optional button, as the engine shows for an empty cart or category.
LinkAn anchor that moves between store pages without reloading, adding the language (and, on localhost or the preview host, the store) to the path.
Source and docs
- Engine source:
packages/theme-engine/src/components/common/ErrorPage.tsx