Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
+68
@@ -0,0 +1,68 @@
|
||||
import "@testing-library/jest-dom";
|
||||
import { MemoryRouter } from "react-router";
|
||||
import { describe, it, vi } from "vitest";
|
||||
import { ErrorPage, type ErrorStatus } from "./ErrorPage";
|
||||
import { expectNoA11yViolations } from "~/utils/test/a11y";
|
||||
|
||||
const translations = vi.hoisted<Record<string, string>>(() => ({
|
||||
"401.eyebrow": "Unauthorized",
|
||||
"401.title": "You're not authorized to see this page.",
|
||||
"401.description": "Contact your admin and verify your permissions.",
|
||||
"403.eyebrow": "Forbidden",
|
||||
"403.title": "You don't have permission to access this page.",
|
||||
"403.description": "If you think this is a mistake, contact your admin or request access.",
|
||||
"404.eyebrow": "404 error",
|
||||
"404.title": "Page not found.",
|
||||
"404.description": "Sorry, we couldn't find the page you're looking for.",
|
||||
"500.eyebrow": "500 error",
|
||||
"500.title": "Something went wrong.",
|
||||
"500.description": "We couldn't complete your request. Please try again or contact support if the issue continues.",
|
||||
"503.eyebrow": "503 error",
|
||||
"503.title": "Maintenance mode.",
|
||||
"503.description": "We're making improvements. Please try again shortly.",
|
||||
goBack: "Go back",
|
||||
"shared.logo": "Logo",
|
||||
}));
|
||||
|
||||
vi.mock("react-router", async () => {
|
||||
const actual = await vi.importActual<typeof import("react-router")>("react-router");
|
||||
return {
|
||||
...actual,
|
||||
useNavigate: () => vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("react-i18next", () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string) => translations[key] ?? key,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("~/components/brand/Logo", () => ({ default: () => <div data-testid="logo">Logo</div> }));
|
||||
vi.mock("~/components/public/PublicFooter", () => ({ default: () => <footer data-testid="public-footer">Footer</footer> }));
|
||||
|
||||
describe("ErrorPage a11y", () => {
|
||||
it.each([401, 403, 404, 500, 503] as ErrorStatus[])("status %s renders without a11y violations", async (status) => {
|
||||
await expectNoA11yViolations(
|
||||
<MemoryRouter>
|
||||
<ErrorPage status={status} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
});
|
||||
|
||||
it("custom back link variant has accessible link text", async () => {
|
||||
await expectNoA11yViolations(
|
||||
<MemoryRouter>
|
||||
<ErrorPage status={401} customBackButton={<a href="/dashboard">Return to dashboard</a>} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
});
|
||||
|
||||
it("with logo image fallback uses alt text", async () => {
|
||||
await expectNoA11yViolations(
|
||||
<MemoryRouter>
|
||||
<ErrorPage status={500} logo="/logo.png" />
|
||||
</MemoryRouter>
|
||||
);
|
||||
});
|
||||
});
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
import "@testing-library/jest-dom";
|
||||
import { fireEvent, render, screen } from "@testing-library/react";
|
||||
import { MemoryRouter } from "react-router";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { ERROR_PAGE_PRESETS, ErrorPage, type ErrorStatus } from "./ErrorPage";
|
||||
|
||||
const navigate = vi.fn();
|
||||
const errorMessages = vi.hoisted<Record<string, string>>(() => ({
|
||||
"401.eyebrow": "Unauthorized",
|
||||
"401.title": "You're not authorized to see this page.",
|
||||
"401.description": "Contact your admin and verify your permissions.",
|
||||
"403.eyebrow": "Forbidden",
|
||||
"403.title": "You don't have permission to access this page.",
|
||||
"403.description": "If you think this is a mistake, contact your admin or request access.",
|
||||
"404.eyebrow": "404 error",
|
||||
"404.title": "Page not found.",
|
||||
"404.description": "Sorry, we couldn't find the page you're looking for.",
|
||||
"500.eyebrow": "500 error",
|
||||
"500.title": "Something went wrong.",
|
||||
"500.description": "We couldn't complete your request. Please try again or contact support if the issue continues.",
|
||||
"503.eyebrow": "503 error",
|
||||
"503.title": "Maintenance mode.",
|
||||
"503.description": "We're making improvements. Please try again shortly.",
|
||||
goBack: "Go back",
|
||||
"shared.logo": "Logo",
|
||||
}));
|
||||
|
||||
const expectedErrorCopy = Object.fromEntries(
|
||||
([401, 403, 404, 500, 503] as ErrorStatus[]).map((status) => [
|
||||
status,
|
||||
{
|
||||
eyebrow: errorMessages[ERROR_PAGE_PRESETS[status].eyebrowKey],
|
||||
title: errorMessages[ERROR_PAGE_PRESETS[status].titleKey],
|
||||
description: errorMessages[ERROR_PAGE_PRESETS[status].descriptionKey],
|
||||
},
|
||||
])
|
||||
) as Record<ErrorStatus, { eyebrow: string; title: string; description: string }>;
|
||||
|
||||
vi.mock("react-router", async () => {
|
||||
const actual = await vi.importActual<typeof import("react-router")>("react-router");
|
||||
return {
|
||||
...actual,
|
||||
useNavigate: () => navigate,
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("react-i18next", () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string) => errorMessages[key] ?? key,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("~/components/brand/Logo", () => ({ default: () => <div data-testid="logo">Logo</div> }));
|
||||
vi.mock("~/components/public/PublicFooter", () => ({ default: () => <footer data-testid="public-footer">Footer</footer> }));
|
||||
|
||||
function renderErrorPage(status: ErrorStatus, props: Partial<React.ComponentProps<typeof ErrorPage>> = {}) {
|
||||
return render(
|
||||
<MemoryRouter>
|
||||
<ErrorPage status={status} {...props} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
}
|
||||
|
||||
describe("ErrorPage", () => {
|
||||
it.each([401, 403, 404, 500, 503] as ErrorStatus[])("renders preset copy and markers for %s", (status) => {
|
||||
renderErrorPage(status);
|
||||
|
||||
const preset = ERROR_PAGE_PRESETS[status];
|
||||
const copy = expectedErrorCopy[status];
|
||||
const page = screen.getByTestId("error-page");
|
||||
expect(page).toHaveAttribute("data-error-status", String(status));
|
||||
expect(preset.eyebrowKey).toBe(`${status}.eyebrow`);
|
||||
expect(screen.getByText(copy.eyebrow)).toBeInTheDocument();
|
||||
expect(screen.getByRole("heading", { name: copy.title })).toBeInTheDocument();
|
||||
expect(screen.getByText(copy.description)).toBeInTheDocument();
|
||||
expect(screen.getByTestId("logo")).toBeInTheDocument();
|
||||
expect(screen.getByTestId("public-footer")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("omits footer and back controls when disabled", () => {
|
||||
renderErrorPage(404, { withFooter: false, withGoBack: false });
|
||||
|
||||
expect(screen.queryByTestId("public-footer")).not.toBeInTheDocument();
|
||||
expect(screen.queryByRole("button", { name: /go back/i })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders a custom back action instead of the default navigation button", () => {
|
||||
renderErrorPage(401, { customBackButton: <a href="/dashboard">Return to dashboard</a> });
|
||||
|
||||
expect(screen.getByRole("link", { name: "Return to dashboard" })).toBeInTheDocument();
|
||||
expect(screen.queryByRole("button", { name: /go back/i })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("navigates back with the default back action", () => {
|
||||
renderErrorPage(403);
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: /go back/i }));
|
||||
|
||||
expect(navigate).toHaveBeenCalledWith(-1);
|
||||
});
|
||||
|
||||
it("does not render diagnostics unless explicitly supplied", () => {
|
||||
const { rerender } = render(
|
||||
<MemoryRouter>
|
||||
<ErrorPage status={500} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
expect(screen.queryByText("stack trace details")).not.toBeInTheDocument();
|
||||
|
||||
rerender(
|
||||
<MemoryRouter>
|
||||
<ErrorPage status={500} diagnosticChildren={<pre>stack trace details</pre>} />
|
||||
</MemoryRouter>
|
||||
);
|
||||
|
||||
expect(screen.getByText("stack trace details")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
import { useNavigate } from "react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import Logo from "~/components/brand/Logo";
|
||||
import PublicFooter from "~/components/public/PublicFooter";
|
||||
|
||||
export type ErrorStatus = 401 | 403 | 404 | 500 | 503;
|
||||
|
||||
export interface ErrorPagePreset {
|
||||
eyebrowKey: string;
|
||||
titleKey: string;
|
||||
descriptionKey: string;
|
||||
}
|
||||
|
||||
export const ERROR_PAGE_PRESETS: Record<ErrorStatus, ErrorPagePreset> = {
|
||||
401: {
|
||||
eyebrowKey: "401.eyebrow",
|
||||
titleKey: "401.title",
|
||||
descriptionKey: "401.description",
|
||||
},
|
||||
403: {
|
||||
eyebrowKey: "403.eyebrow",
|
||||
titleKey: "403.title",
|
||||
descriptionKey: "403.description",
|
||||
},
|
||||
404: {
|
||||
eyebrowKey: "404.eyebrow",
|
||||
titleKey: "404.title",
|
||||
descriptionKey: "404.description",
|
||||
},
|
||||
500: {
|
||||
eyebrowKey: "500.eyebrow",
|
||||
titleKey: "500.title",
|
||||
descriptionKey: "500.description",
|
||||
},
|
||||
503: {
|
||||
eyebrowKey: "503.eyebrow",
|
||||
titleKey: "503.title",
|
||||
descriptionKey: "503.description",
|
||||
},
|
||||
};
|
||||
|
||||
export interface ErrorPageProps {
|
||||
status: ErrorStatus;
|
||||
title?: string;
|
||||
description?: string;
|
||||
eyebrow?: string;
|
||||
withLogo?: boolean;
|
||||
withFooter?: boolean;
|
||||
withGoBack?: boolean;
|
||||
customBackButton?: React.ReactNode;
|
||||
logo?: string | undefined;
|
||||
actionSlot?: React.ReactNode;
|
||||
diagnosticChildren?: React.ReactNode;
|
||||
}
|
||||
|
||||
export function ErrorPage({
|
||||
status,
|
||||
title,
|
||||
description,
|
||||
eyebrow,
|
||||
withLogo = true,
|
||||
withFooter = true,
|
||||
withGoBack = true,
|
||||
customBackButton,
|
||||
logo,
|
||||
actionSlot,
|
||||
diagnosticChildren,
|
||||
}: ErrorPageProps) {
|
||||
const navigate = useNavigate();
|
||||
const { t } = useTranslation("errors");
|
||||
const preset = ERROR_PAGE_PRESETS[status];
|
||||
const resolvedEyebrow = eyebrow ?? t(preset.eyebrowKey);
|
||||
const resolvedTitle = title ?? t(preset.titleKey);
|
||||
const resolvedDescription = description ?? t(preset.descriptionKey);
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="" data-error-status={status} data-testid="error-page">
|
||||
<div className="flex min-h-full flex-col pt-16 pb-12">
|
||||
<main className="mx-auto flex w-full max-w-7xl min-w-0 grow flex-col justify-center px-4 sm:px-6 lg:px-8">
|
||||
{withLogo && (
|
||||
<div className="flex shrink-0 justify-center">
|
||||
{logo ? <img className="h-9 w-auto" src={logo} alt={t("shared.logo", { ns: "translations" })} /> : <Logo />}
|
||||
</div>
|
||||
)}
|
||||
<div className="py-16">
|
||||
<div className="min-w-0 text-center [overflow-wrap:anywhere]">
|
||||
<p className="text-brand-brand-surface-solid-default text-sm font-semibold tracking-wide uppercase">{resolvedEyebrow}</p>
|
||||
<h1 className="mt-2 text-4xl font-extrabold tracking-tight sm:text-5xl">{resolvedTitle}</h1>
|
||||
<p className="text-text-neutral-tertiary mt-2 text-base">{resolvedDescription}</p>
|
||||
{(withGoBack || customBackButton || actionSlot) && (
|
||||
<div className="mt-4 min-w-0">
|
||||
{customBackButton}
|
||||
{!customBackButton && withGoBack && (
|
||||
<button type="button" onClick={() => navigate(-1)} className="text-brand-brand-surface-solid-default hover:text-brand-brand-surface-solid-hover w-full text-center text-sm font-medium">
|
||||
<span aria-hidden="true"> ←</span> {t("goBack")}
|
||||
</button>
|
||||
)}
|
||||
{actionSlot}
|
||||
</div>
|
||||
)}
|
||||
{diagnosticChildren && <div className="mt-6 text-left">{diagnosticChildren}</div>}
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
{withFooter && <PublicFooter />}
|
||||
</>
|
||||
);
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
import "@testing-library/jest-dom";
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { MemoryRouter } from "react-router";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { ERROR_PAGE_PRESETS, type ErrorStatus } from "./ErrorPage";
|
||||
import ServerError from "./ServerError";
|
||||
|
||||
let routeError: unknown;
|
||||
const errorMessages = vi.hoisted<Record<string, string>>(() => ({
|
||||
"401.eyebrow": "Unauthorized",
|
||||
"401.title": "You're not authorized to see this page.",
|
||||
"401.description": "Contact your admin and verify your permissions.",
|
||||
"403.eyebrow": "Forbidden",
|
||||
"403.title": "You don't have permission to access this page.",
|
||||
"403.description": "If you think this is a mistake, contact your admin or request access.",
|
||||
"404.eyebrow": "404 error",
|
||||
"404.title": "Page not found.",
|
||||
"404.description": "Sorry, we couldn't find the page you're looking for.",
|
||||
"500.eyebrow": "500 error",
|
||||
"500.title": "Something went wrong.",
|
||||
"500.description": "We couldn't complete your request. Please try again or contact support if the issue continues.",
|
||||
"errors:devCompile.reloadTitle": "Compiling... reload in a few seconds.",
|
||||
"errors:devCompile.reloadDescription": "This error is usually temporary and should be resolved by reloading the page.",
|
||||
goBack: "Go back",
|
||||
"shared.logo": "Logo",
|
||||
}));
|
||||
|
||||
const expectedErrorCopy = Object.fromEntries(
|
||||
([401, 403, 404, 500] as ErrorStatus[]).map((status) => [
|
||||
status,
|
||||
{
|
||||
eyebrow: errorMessages[ERROR_PAGE_PRESETS[status].eyebrowKey],
|
||||
title: errorMessages[ERROR_PAGE_PRESETS[status].titleKey],
|
||||
description: errorMessages[ERROR_PAGE_PRESETS[status].descriptionKey],
|
||||
},
|
||||
])
|
||||
) as Record<ErrorStatus, { eyebrow: string; title: string; description: string }>;
|
||||
|
||||
vi.mock("react-router", async () => {
|
||||
const actual = await vi.importActual<typeof import("react-router")>("react-router");
|
||||
return {
|
||||
...actual,
|
||||
useNavigate: () => vi.fn(),
|
||||
useRouteError: () => routeError,
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("react-i18next", () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string) => errorMessages[key] ?? key,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("i18next", () => ({
|
||||
t: (key: string) => errorMessages[key] ?? key,
|
||||
}));
|
||||
|
||||
vi.mock("~/components/brand/Logo", () => ({ default: () => <div data-testid="logo">Logo</div> }));
|
||||
vi.mock("~/components/public/PublicFooter", () => ({ default: () => <footer data-testid="public-footer">Footer</footer> }));
|
||||
|
||||
function renderServerError(error: unknown) {
|
||||
routeError = error;
|
||||
return render(
|
||||
<MemoryRouter>
|
||||
<ServerError />
|
||||
</MemoryRouter>
|
||||
);
|
||||
}
|
||||
|
||||
describe("ServerError", () => {
|
||||
beforeEach(() => {
|
||||
routeError = undefined;
|
||||
});
|
||||
|
||||
it.each([404, 401, 403] as ErrorStatus[])("maps thrown %s statuses to the shared error page", (status) => {
|
||||
renderServerError({ status });
|
||||
|
||||
const copy = expectedErrorCopy[status];
|
||||
expect(screen.getByTestId("error-page")).toHaveAttribute("data-error-status", String(status));
|
||||
expect(screen.getByText(copy.eyebrow)).toBeInTheDocument();
|
||||
expect(screen.getByRole("heading", { name: copy.title })).toBeInTheDocument();
|
||||
expect(screen.getByText(copy.description)).toBeInTheDocument();
|
||||
expect(screen.queryByTestId("public-footer")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("falls back to the shared 500 page for generic errors", () => {
|
||||
renderServerError(new Error("Database connection failed"));
|
||||
|
||||
const copy = expectedErrorCopy[500];
|
||||
expect(screen.getByTestId("error-page")).toHaveAttribute("data-error-status", "500");
|
||||
expect(screen.getByText(copy.eyebrow)).toBeInTheDocument();
|
||||
expect(screen.getByRole("heading", { name: copy.title })).toBeInTheDocument();
|
||||
expect(screen.getByText(copy.description)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("redacts raw stack and error message text from rendered 500 pages", () => {
|
||||
const error = new Error("Sensitive database failure");
|
||||
error.stack = "SensitiveStackTrace: SELECT * FROM secrets";
|
||||
|
||||
renderServerError(error);
|
||||
|
||||
expect(screen.getByTestId("error-page")).toHaveAttribute("data-error-status", "500");
|
||||
expect(screen.queryByText("Sensitive database failure")).not.toBeInTheDocument();
|
||||
expect(screen.queryByText(/SensitiveStackTrace/)).not.toBeInTheDocument();
|
||||
expect(screen.queryByText(/SELECT \* FROM secrets/)).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("keeps known temporary compile-error copy without rendering stack details", () => {
|
||||
const error = new Error("Cannot read properties of null (reading 'useContext')");
|
||||
error.stack = "React internals stack";
|
||||
|
||||
renderServerError(error);
|
||||
|
||||
expect(screen.getByTestId("error-page")).toHaveAttribute("data-error-status", "500");
|
||||
expect(screen.getByRole("heading", { name: "Compiling... reload in a few seconds." })).toBeInTheDocument();
|
||||
expect(screen.getByText("This error is usually temporary and should be resolved by reloading the page.")).toBeInTheDocument();
|
||||
expect(screen.queryByText("React internals stack")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("normalizes unsupported statuses to the shared 500 fallback", () => {
|
||||
renderServerError({ status: 418, data: { message: "Teapot details" } });
|
||||
|
||||
expect(screen.getByTestId("error-page")).toHaveAttribute("data-error-status", "500");
|
||||
expect(screen.getByRole("heading", { name: expectedErrorCopy[500].title })).toBeInTheDocument();
|
||||
expect(screen.queryByText("Teapot details")).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
import { isRouteErrorResponse, useRouteError } from "react-router";
|
||||
import { t as i18nT } from "i18next";
|
||||
import { ErrorPage, type ErrorStatus } from "~/components/ui/errors/ErrorPage";
|
||||
|
||||
type KnownErrorCopy = { title: string; description?: string };
|
||||
|
||||
export default function ServerError() {
|
||||
const error = useRouteError();
|
||||
const status = getErrorStatus(error);
|
||||
const knownError = getKnownErrorCopy(error);
|
||||
|
||||
return <ErrorPage status={status} title={knownError?.title} description={knownError?.description} withFooter={false} />;
|
||||
}
|
||||
|
||||
function getErrorStatus(error: unknown): ErrorStatus {
|
||||
if (isRouteErrorResponse(error)) {
|
||||
return normalizeStatus(error.status);
|
||||
}
|
||||
|
||||
if (typeof error === "object" && error !== null && "status" in error) {
|
||||
return normalizeStatus((error as { status?: unknown }).status);
|
||||
}
|
||||
|
||||
return 500;
|
||||
}
|
||||
|
||||
function normalizeStatus(status: unknown): ErrorStatus {
|
||||
return status === 401 || status === 403 || status === 404 || status === 500 ? status : 500;
|
||||
}
|
||||
|
||||
function getKnownErrorCopy(error: unknown): KnownErrorCopy | undefined {
|
||||
const message = getErrorMessage(error);
|
||||
return message ? KNOWN_ERRORS[message] : undefined;
|
||||
}
|
||||
|
||||
function getErrorMessage(error: unknown): string | undefined {
|
||||
if (isRouteErrorResponse(error)) {
|
||||
const data = error.data;
|
||||
if (typeof data === "string") {
|
||||
return data;
|
||||
}
|
||||
if (typeof data === "object" && data !== null) {
|
||||
const message = (data as { message?: unknown; error?: unknown }).message ?? (data as { message?: unknown; error?: unknown }).error;
|
||||
return typeof message === "string" ? message : undefined;
|
||||
}
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
}
|
||||
|
||||
if (typeof error === "object" && error !== null) {
|
||||
const message = (error as { message?: unknown }).message;
|
||||
return typeof message === "string" ? message : undefined;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const KNOWN_ERRORS: Record<string, KnownErrorCopy> = {
|
||||
"Cannot read properties of null (reading 'useContext')": {
|
||||
title: i18nT("errors:devCompile.reloadTitle"),
|
||||
description: i18nT("errors:devCompile.reloadDescription"),
|
||||
},
|
||||
"Cannot read properties of null (reading 'useRef')": {
|
||||
title: i18nT("errors:devCompile.reloadTitle"),
|
||||
description: i18nT("errors:devCompile.reloadDescription"),
|
||||
},
|
||||
"Cannot read properties of null (reading 'useState')": {
|
||||
title: i18nT("errors:devCompile.reloadTitle"),
|
||||
description: i18nT("errors:devCompile.reloadDescription"),
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user