Compare commits
2
Commits
98c7701ccd
...
f266bda3a9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f266bda3a9 | ||
|
|
88ab2561bd |
No files matched your search
@@ -41,6 +41,7 @@ import { Route, Routes } from "react-router-dom";
|
|||||||
import { AdminOverviewRedirect } from "./AdminRouter.js";
|
import { AdminOverviewRedirect } from "./AdminRouter.js";
|
||||||
import { createBrandRouteElement } from "./route-access.js";
|
import { createBrandRouteElement } from "./route-access.js";
|
||||||
import { Suspense } from "react";
|
import { Suspense } from "react";
|
||||||
|
import { AdminErrorBoundary } from "../components/AdminErrorBoundary.js";
|
||||||
|
|
||||||
export function Dashboard({
|
export function Dashboard({
|
||||||
onLogout,
|
onLogout,
|
||||||
@@ -146,37 +147,39 @@ export function Dashboard({
|
|||||||
</AdminLayout.Header>
|
</AdminLayout.Header>
|
||||||
<AdminLayout.Content className="admin-content">
|
<AdminLayout.Content className="admin-content">
|
||||||
<div className="admin-content-inner">
|
<div className="admin-content-inner">
|
||||||
<Suspense fallback={<LoadingStack />}>
|
<AdminErrorBoundary key={location.pathname}>
|
||||||
<Routes>
|
<Suspense fallback={<LoadingStack />}>
|
||||||
<Route index element={<AdminOverviewRedirect />} />
|
<Routes>
|
||||||
<Route path="overview" element={<FeatureOverviewPage />} />
|
<Route index element={<AdminOverviewRedirect />} />
|
||||||
<Route path="publication" element={<FeaturePublicationPage />} />
|
<Route path="overview" element={<FeatureOverviewPage />} />
|
||||||
<Route path="brand" element={<FeatureBrandPage />} />
|
<Route path="publication" element={<FeaturePublicationPage />} />
|
||||||
<Route path="trust" element={<FeatureTrustContentPage />} />
|
<Route path="brand" element={<FeatureBrandPage />} />
|
||||||
<Route path="services" element={<FeatureServicesPage />} />
|
<Route path="trust" element={<FeatureTrustContentPage />} />
|
||||||
<Route path="inquiries" element={<FeatureInquiriesPage />} />
|
<Route path="services" element={<FeatureServicesPage />} />
|
||||||
<Route path="workspace" element={<FeatureWorkspacePage />} />
|
<Route path="inquiries" element={<FeatureInquiriesPage />} />
|
||||||
{role === "editor" ? null : (
|
<Route path="workspace" element={<FeatureWorkspacePage />} />
|
||||||
<>
|
{role === "editor" ? null : (
|
||||||
<Route
|
<>
|
||||||
path="brands"
|
<Route
|
||||||
element={<FeatureBrandsPage canCreate={role === "owner"} />}
|
path="brands"
|
||||||
/>
|
element={<FeatureBrandsPage canCreate={role === "owner"} />}
|
||||||
<Route
|
/>
|
||||||
path="brands/new"
|
<Route
|
||||||
element={createBrandRouteElement(role, <FeatureCreateBrandPage />)}
|
path="brands/new"
|
||||||
/>
|
element={createBrandRouteElement(role, <FeatureCreateBrandPage />)}
|
||||||
<Route path="page-scheme" element={<FeaturePageSchemePage />} />
|
/>
|
||||||
<Route path="capabilities" element={<FeatureCapabilitiesPage />} />
|
<Route path="page-scheme" element={<FeaturePageSchemePage />} />
|
||||||
<Route path="members" element={<FeatureMembersPage />} />
|
<Route path="capabilities" element={<FeatureCapabilitiesPage />} />
|
||||||
{platformTemplateRole === "template_admin" ? (
|
<Route path="members" element={<FeatureMembersPage />} />
|
||||||
<Route path="templates" element={<FeatureTemplateManagementPage />} />
|
{platformTemplateRole === "template_admin" ? (
|
||||||
) : null}
|
<Route path="templates" element={<FeatureTemplateManagementPage />} />
|
||||||
</>
|
) : null}
|
||||||
)}
|
</>
|
||||||
<Route path="*" element={<AdminOverviewRedirect />} />
|
)}
|
||||||
</Routes>
|
<Route path="*" element={<AdminOverviewRedirect />} />
|
||||||
</Suspense>
|
</Routes>
|
||||||
|
</Suspense>
|
||||||
|
</AdminErrorBoundary>
|
||||||
</div>
|
</div>
|
||||||
</AdminLayout.Content>
|
</AdminLayout.Content>
|
||||||
</AdminLayout>
|
</AdminLayout>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { AdminRouter } from "./AdminRouter.js";
|
|||||||
import { App } from "antd";
|
import { App } from "antd";
|
||||||
import { createApiClient } from "../lib/api.js";
|
import { createApiClient } from "../lib/api.js";
|
||||||
import { LoadingStack } from "../components/LoadingStack.js";
|
import { LoadingStack } from "../components/LoadingStack.js";
|
||||||
|
import { AdminErrorBoundary } from "../components/AdminErrorBoundary.js";
|
||||||
|
|
||||||
type SessionState = Omit<AdminSession, "authenticated">;
|
type SessionState = Omit<AdminSession, "authenticated">;
|
||||||
|
|
||||||
@@ -73,12 +74,14 @@ export function Root() {
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
return (
|
return (
|
||||||
<AdminRouter>
|
<AdminErrorBoundary variant="page">
|
||||||
<Dashboard
|
<AdminRouter>
|
||||||
onLogout={logout}
|
<Dashboard
|
||||||
role={session.role}
|
onLogout={logout}
|
||||||
platformTemplateRole={session.platformTemplateRole}
|
role={session.role}
|
||||||
/>
|
platformTemplateRole={session.platformTemplateRole}
|
||||||
</AdminRouter>
|
/>
|
||||||
|
</AdminRouter>
|
||||||
|
</AdminErrorBoundary>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
import { Component, type ErrorInfo, type ReactNode } from "react";
|
||||||
|
import { Button, Result, Space, Typography } from "antd";
|
||||||
|
|
||||||
|
interface AdminErrorBoundaryProps {
|
||||||
|
children: ReactNode;
|
||||||
|
variant?: "page" | "content";
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AdminErrorBoundaryState {
|
||||||
|
error: Error | null;
|
||||||
|
errorId: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createErrorId(): string {
|
||||||
|
return `admin-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 后台渲染错误边界:页面级兜底整页,内容级兜底单个业务页面且保留导航。
|
||||||
|
* React 只支持 class 组件捕获渲染异常,因此这里保留 class 实现。
|
||||||
|
*/
|
||||||
|
export class AdminErrorBoundary extends Component<
|
||||||
|
AdminErrorBoundaryProps,
|
||||||
|
AdminErrorBoundaryState
|
||||||
|
> {
|
||||||
|
override state: AdminErrorBoundaryState = { error: null, errorId: null };
|
||||||
|
|
||||||
|
static getDerivedStateFromError(error: Error): AdminErrorBoundaryState {
|
||||||
|
return { error, errorId: createErrorId() };
|
||||||
|
}
|
||||||
|
|
||||||
|
override componentDidCatch(error: Error, info: ErrorInfo) {
|
||||||
|
console.error("[admin] 页面渲染异常", this.state.errorId, error, info.componentStack);
|
||||||
|
}
|
||||||
|
|
||||||
|
private reset = () => {
|
||||||
|
this.setState({ error: null, errorId: null });
|
||||||
|
};
|
||||||
|
|
||||||
|
private reload = () => {
|
||||||
|
window.location.reload();
|
||||||
|
};
|
||||||
|
|
||||||
|
override render() {
|
||||||
|
const { error, errorId } = this.state;
|
||||||
|
if (!error) return this.props.children;
|
||||||
|
const isPage = this.props.variant === "page";
|
||||||
|
return (
|
||||||
|
<Result
|
||||||
|
status="error"
|
||||||
|
title="页面渲染出错"
|
||||||
|
subTitle={
|
||||||
|
isPage
|
||||||
|
? "后台页面无法显示。可以重新加载;如果问题持续,请把错误编号提供给管理员。"
|
||||||
|
: "这个页面无法显示,其他页面仍可正常使用。可以重试或重新加载页面。"
|
||||||
|
}
|
||||||
|
extra={
|
||||||
|
<Space wrap>
|
||||||
|
{isPage ? null : (
|
||||||
|
<Button type="primary" onClick={this.reset}>
|
||||||
|
重试
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
<Button type={isPage ? "primary" : "default"} onClick={this.reload}>
|
||||||
|
重新加载页面
|
||||||
|
</Button>
|
||||||
|
{isPage ? <Button href="/admin/overview">返回概览</Button> : null}
|
||||||
|
</Space>
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Typography.Paragraph type="secondary" copyable={{ text: errorId ?? "" }}>
|
||||||
|
错误编号:{errorId}
|
||||||
|
</Typography.Paragraph>
|
||||||
|
{import.meta.env.DEV ? (
|
||||||
|
<Typography.Paragraph type="secondary">
|
||||||
|
开发环境错误信息:{error.message}
|
||||||
|
</Typography.Paragraph>
|
||||||
|
) : null}
|
||||||
|
</Result>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
import { test, expect, type Page, type Route } from "@playwright/test";
|
||||||
|
|
||||||
|
const adminOrigin = process.env.ADMIN_URL || "http://localhost:3005";
|
||||||
|
|
||||||
|
const pageErrors = new WeakMap<Page, string[]>();
|
||||||
|
|
||||||
|
test.beforeEach(({ page }) => {
|
||||||
|
const errors: string[] = [];
|
||||||
|
pageErrors.set(page, errors);
|
||||||
|
page.on("pageerror", (error) => errors.push(error.message));
|
||||||
|
});
|
||||||
|
|
||||||
|
test.afterEach(({ page }) => {
|
||||||
|
expect(pageErrors.get(page) ?? []).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
interface MockOptions {
|
||||||
|
/** 品牌成员列表的异常返回,用于触发整页渲染错误 */
|
||||||
|
memberships?: unknown;
|
||||||
|
/** 只在首次请求时返回的异常信任内容,重新加载后恢复 */
|
||||||
|
trustContentOnce?: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function mockAdminApi(page: Page, options: MockOptions = {}) {
|
||||||
|
let servedTrustContent = false;
|
||||||
|
|
||||||
|
await page.route("**/api/**", async (route: Route) => {
|
||||||
|
const request = route.request();
|
||||||
|
const path = new URL(request.url()).pathname.replace(/^\/api/, "");
|
||||||
|
const method = request.method();
|
||||||
|
|
||||||
|
if (path === "/admin/session/current") {
|
||||||
|
return route.fulfill({
|
||||||
|
json: {
|
||||||
|
authenticated: true,
|
||||||
|
displayName: "品牌负责人",
|
||||||
|
role: "owner",
|
||||||
|
mustChangePassword: false,
|
||||||
|
platformTemplateRole: "none",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (path === "/admin/config/brand") {
|
||||||
|
return route.fulfill({
|
||||||
|
json: {
|
||||||
|
id: "brand-current",
|
||||||
|
name: "独立工坊",
|
||||||
|
slug: "atelier-studio",
|
||||||
|
tagline: "把耐心做进作品",
|
||||||
|
templateKey: "personal",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (path === "/admin/brands" && method === "GET") {
|
||||||
|
return route.fulfill({
|
||||||
|
json: options.memberships ?? [
|
||||||
|
{
|
||||||
|
id: "membership-current",
|
||||||
|
role: "owner",
|
||||||
|
brand: {
|
||||||
|
id: "brand-current",
|
||||||
|
name: "独立工坊",
|
||||||
|
slug: "atelier-studio",
|
||||||
|
templateKey: "personal",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (path === "/admin/config/trust-content" && method === "GET") {
|
||||||
|
if (!servedTrustContent && options.trustContentOnce !== undefined) {
|
||||||
|
servedTrustContent = true;
|
||||||
|
return route.fulfill({ json: options.trustContentOnce });
|
||||||
|
}
|
||||||
|
return route.fulfill({ json: [] });
|
||||||
|
}
|
||||||
|
if (path === "/admin/workspace") {
|
||||||
|
return route.fulfill({
|
||||||
|
json: {
|
||||||
|
brand: { id: "brand-current", name: "独立工坊" },
|
||||||
|
installations: [],
|
||||||
|
templateContent: { displayName: "独立创作者画册" },
|
||||||
|
offerings: [],
|
||||||
|
forms: [],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (path === "/admin/releases/readiness") {
|
||||||
|
return route.fulfill({ json: { ready: true, checks: [] } });
|
||||||
|
}
|
||||||
|
return route.fulfill({ json: [] });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test("品牌成员接口返回异常结构时显示整页错误而不是白屏", async ({ page }) => {
|
||||||
|
await page.setViewportSize({ width: 1440, height: 1000 });
|
||||||
|
await mockAdminApi(page, { memberships: [{}] });
|
||||||
|
|
||||||
|
await page.goto(`${adminOrigin}/admin/publication`);
|
||||||
|
|
||||||
|
await expect(page.getByText("页面渲染出错")).toBeVisible();
|
||||||
|
await expect(page.getByText(/错误编号:admin-/)).toBeVisible();
|
||||||
|
await expect(page.getByRole("button", { name: "重新加载页面" })).toBeVisible();
|
||||||
|
await expect(page.getByRole("link", { name: "返回概览" })).toBeVisible();
|
||||||
|
|
||||||
|
const bodyText = await page.locator("body").innerText();
|
||||||
|
expect(bodyText.trim().length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("内容区渲染出错时保留导航,重新加载后恢复", async ({ page }) => {
|
||||||
|
await page.setViewportSize({ width: 1440, height: 1000 });
|
||||||
|
await mockAdminApi(page, { trustContentOnce: [null] });
|
||||||
|
|
||||||
|
await page.goto(`${adminOrigin}/admin/trust`);
|
||||||
|
|
||||||
|
await expect(page.getByText("页面渲染出错")).toBeVisible();
|
||||||
|
await expect(page.getByText(/错误编号:admin-/)).toBeVisible();
|
||||||
|
|
||||||
|
// 外壳导航仍然可用,不会因为单个页面崩溃而整站白屏
|
||||||
|
await expect(page.getByRole("button", { name: "退出登录" })).toBeVisible();
|
||||||
|
await expect(page.getByText("内容与发布")).toBeVisible();
|
||||||
|
|
||||||
|
// 重试仍失败时继续展示错误页,而不是变回白屏
|
||||||
|
await page.getByRole("button", { name: /重\s*试/ }).click();
|
||||||
|
await expect(page.getByText("页面渲染出错")).toBeVisible();
|
||||||
|
|
||||||
|
await page.getByRole("button", { name: "重新加载页面" }).click();
|
||||||
|
await expect(page.getByRole("heading", { name: "信任内容" })).toBeVisible();
|
||||||
|
await expect(page.getByText("页面渲染出错")).toBeHidden();
|
||||||
|
});
|
||||||
Reference in new issue
Block a user