หน้าตัวอย่าง
ระบบขัดข้อง 5xx
ใช้เมื่อเกิดข้อผิดพลาดที่ไม่คาดคิดระหว่างแสดงหน้า เช่น API หลักตอบ 5xx หมดเวลา หรือเครือข่ายขัดข้อง error.tsx แสดงในโครงแอป ส่วน global-error.tsx แทนทั้งหน้าเมื่อเลย์เอาต์รากพัง ข้อผิดพลาดที่ผู้ใช้แก้ได้ เช่น ข้อมูลไม่ถูกต้อง แสดงที่ช่องกรอกหรือด้วย Alert ไม่ใช่หน้านี้ ส่วนตารางที่โหลดไม่ได้ใช้ TableState
คอมโพเนนต์ที่ใช้
- StatusPageA page for an outcome instead of the content: the page wasn't found, the person may not open it, the server failed, or the system is closed for maintenance.
- ButtonA button, or a link that looks like one with href.
- CopyButtonCopies a value, such as a document number, a link or code, to the clipboard.
- AppShellThe application frame: the left menu, which collapses to icons on wide screens and slides in on phones, and the top bar with the breadcrumb, global search, language switch, notifications and account menu.
import { StatusPage, Button, CopyButton, AppShell } from "@bsru/ui";กฎของหน้านี้
ถ้อยคำ
- บอกว่าระบบขัดข้อง ไม่ใช่ผู้ใช้ทำผิด และบอกว่าทำอะไรต่อ: ลองอีกครั้ง แล้วแจ้งผู้ดูแลระบบพร้อมรหัสอ้างอิง
- ถ้ารู้ว่าส่วนไหนขัดข้อง เช่น ระบบหลักไม่ตอบสนอง บอกด้วยคำที่ผู้ใช้เข้าใจ
- บอกว่าข้อมูลที่บันทึกไปแล้วยังอยู่ เมื่อรู้แน่ว่าเป็นอย่างนั้น
ปุ่มและทางไปต่อ
- ปุ่มหลัก: ลองอีกครั้ง ซึ่งเรียก reset() ของ Next.js ปุ่มรอง: กลับหน้าแรก
- แสดงรหัสอ้างอิง (Correlation ID) ทุกครั้งพร้อมปุ่มคัดลอก ตามมาตรฐาน frontend
ห้ามแสดง
- stack trace ข้อความ error ดิบ ชื่อ exception ชื่อเซิร์ฟเวอร์ ที่อยู่ภายใน SQL หรือเนื้อหาของคำขอและคำตอบ
- รายละเอียดทางเทคนิคแม้ในระบบทดสอบ ใช้รหัสอ้างอิงค้นใน log แทน
ในแอป
- บันทึก log สถานะ code และ Correlation ID ของคำขอที่ล้มเหลว ไม่บันทึกเนื้อหาของคำขอหรือคำตอบ เพราะอาจมีข้อมูลส่วนบุคคล
- global-error.tsx ไม่มีไฟล์ข้อความของแอป จึงเขียนคำทั้งสองภาษาไว้ในไฟล์ และใส่ LangProvider เอง
ตัวอย่างทั้งหน้า
แต่ละตัวอย่างเป็นหน้าเต็ม แสดงในกรอบจอคอมพิวเตอร์ เลือกโทรศัพท์หรือแท็บเล็ตที่แถบบนเพื่อดูบนจอนั้น ข้อมูลทั้งหมดเป็นข้อมูลสมมติ
ERP: error.tsx ในโครงแอป
API หลักตอบ 500 ขณะโหลดรายการ error.tsx แสดงใน AppShell พร้อมรหัสอ้างอิงและปุ่มคัดลอก ลองอีกครั้งเรียก reset()
// app/(erp)/error.tsx: an unexpected error while rendering a page, shown inside the ERP's AppShell.
"use client";
import { Button, StatusPage } from "@bsru/ui";
import { House, RotateCw } from "lucide-react";
import { useTranslations } from "next-intl";
import { useEffect } from "react";
import { useCorrelationId } from "@/components/correlation"; // The request's X-Correlation-Id, passed down by the root layout
export default function ErrorPage({ error, reset }: { error: Error & { digest?: string }; reset: () => void }) {
const t = useTranslations();
const correlationId = useCorrelationId() ?? error.digest;
useEffect(() => {
// The status, code and Correlation ID only. Never the request or response body.
reportError({ correlationId, digest: error.digest });
}, [correlationId, error.digest]);
return (
<StatusPage
kind="serverError"
fill={false}
reference={correlationId ? { label: t("status.reference"), value: correlationId } : undefined}
actions={
<>
<Button tone="primary" icon={RotateCw} label={t("status.retry")} onClick={() => reset()} />
<Button icon={House} label={t("status.home")} href="/" />
</>
}
/>
);
}| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| kind* จำเป็น | "serverError" | StatusKind ค่าที่ใช้ได้: "notFound""forbidden""serverError""maintenance" | notFound for 404, forbidden for 403, serverError for 500 and the other 5xx, maintenance for a planned closure. |
| fill | false | boolean | true fills the screen as a page of its own, with its own main landmark. false sits in AppShell's content area, in the app's main. |
| reference | ออบเจกต์ 2 ฟิลด์{ label: "รหัสอ้างอิง", value: "0193a5e2-7c4d-7b21-9f00-3c5a1d2e4b6f" } | StatusReference | For a server error: the Correlation ID, with a button that copies it, so support can trace the problem. |
| actions | <>
<Button
tone="primary"
icon={RotateCw}
label="ลองอีกครั้ง"
onClick={handleClick}
/>
<Button icon={House} label="กลับหน้าแรก" href="/" />
</> | ReactNode | The ways forward, as Button components, with the primary one first, such as Back to the home page. |
คอนโซล: global-error.tsx เต็มหน้าจอ
เลย์เอาต์รากโหลดไม่ได้เพราะระบบหลักไม่ตอบสนอง หน้าเต็มจอแทนทั้งแอป code แสดง 503 แทน 500 หัวข้อบอกว่าส่วนไหนขัดข้อง
// app/global-error.tsx: an error in the root layout itself. It replaces the root layout, so it renders html
// and body, and its own LangProvider.
"use client";
import { Button, LangProvider, StatusPage } from "@bsru/ui";
import { RotateCw } from "lucide-react";
import "./globals.css";
// No message files load here, so the words are written in both languages, picked by the page's language.
export default function GlobalError({ error, reset }: { error: Error & { digest?: string }; reset: () => void }) {
const lang = readLocaleCookie(); // "th" or "en"
const t = (th: string, en: string) => (lang === "en" ? en : th);
const isDemo = process.env.NEXT_PUBLIC_DEMO === "1";
return (
<html lang={lang}>
<body>
<LangProvider locale={lang}>
<StatusPage
kind="serverError"
code="503"
brand={{ name: "BSRU ERP คอนโซลผู้ดูแลระบบ", caption: t("มหาวิทยาลัยราชภัฏบ้านสมเด็จเจ้าพระยา", "Bansomdejchaopraya Rajabhat University"), logo: "/bsru-logo.png", alt: t("ตรามหาวิทยาลัย", "University seal") }}
notice={isDemo ? t("ระบบจำลอง ใช้ข้อมูลทดสอบเท่านั้น", "Demo system, test data only") : undefined}
title={t("ระบบหลักไม่ตอบสนอง", "The core system isn't answering")}
description={t("ลองอีกครั้งในอีกสักครู่ ถ้ายังไม่สำเร็จ แจ้งทีมดูแลระบบพร้อมรหัสอ้างอิงด้านล่าง", "Try again in a moment. If it still fails, tell the support team and give them the reference below.")}
reference={error.digest ? { value: error.digest } : undefined}
actions={<Button tone="primary" icon={RotateCw} label={t("ลองอีกครั้ง", "Try again")} onClick={() => reset()} />}
/>
</LangProvider>
</body>
</html>
);
}| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| kind* จำเป็น | "serverError" | StatusKind ค่าที่ใช้ได้: "notFound""forbidden""serverError""maintenance" | notFound for 404, forbidden for 403, serverError for 500 and the other 5xx, maintenance for a planned closure. |
| code | "503" | string | null | The code shown above the title. Defaults to 404, 403 or 500 for the kind, and none for maintenance. Pass 503, say, to change it, or null to hide it. |
| brand | ออบเจกต์ 4 ฟิลด์{
name: "BSRU ERP คอนโซลผู้ดูแลระบบ",
caption: "มหาวิทยาลัยราชภัฏบ้านสมเด็จเจ้าพระยา",
logo: "/bsru-logo.png",
alt: "ตรามหาวิทยาลัยราชภัฏบ้านสมเด็จเจ้าพระยา",
} | PageBrand | The logo and system name at the top of a whole page. Leave it out inside AppShell, which shows its own. |
| notice | "ระบบจำลอง ใช้ข้อมูลทดสอบเท่านั้น" | string | The gold strip across the top of a whole page, for demo and test systems, such as ระบบจำลอง. |
| title | "ระบบหลักไม่ตอบสนอง" | string | What happened, the page's level 1 heading. Defaults to a title for the kind in the screen language. |
| description | "คอนโซลติดต่อระบบหลักไม่ได้ในตอนนี้ ลองอีกครั้งในอีกสักครู่ ถ้ายังไม่สำเร็จ แจ้งทีมดูแลระบบพร้อมรหัสอ้างอิงด้านล่าง" | string | What to do next, in a sentence or two. Defaults to a sentence for the kind in the screen language. |
| reference | ออบเจกต์ 2 ฟิลด์{ label: "รหัสอ้างอิง", value: "0193a5e2-7c4d-7b21-9f00-3c5a1d2e4b6f" } | StatusReference | For a server error: the Correlation ID, with a button that copies it, so support can trace the problem. |