ข้ามไปยังเนื้อหา

หน้าตัวอย่าง

ระบบขัดข้อง 5xx

ใช้เมื่อเกิดข้อผิดพลาดที่ไม่คาดคิดระหว่างแสดงหน้า เช่น API หลักตอบ 5xx หมดเวลา หรือเครือข่ายขัดข้อง error.tsx แสดงในโครงแอป ส่วน global-error.tsx แทนทั้งหน้าเมื่อเลย์เอาต์รากพัง ข้อผิดพลาดที่ผู้ใช้แก้ได้ เช่น ข้อมูลไม่ถูกต้อง แสดงที่ช่องกรอกหรือด้วย Alert ไม่ใช่หน้านี้ ส่วนตารางที่โหลดไม่ได้ใช้ TableState

คอมโพเนนต์ที่ใช้

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()

จอคอมพิวเตอร์ 1280 × 800 px
// 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="/" />
        </>
      }
    />
  );
}

คอนโซล: global-error.tsx เต็มหน้าจอ

เลย์เอาต์รากโหลดไม่ได้เพราะระบบหลักไม่ตอบสนอง หน้าเต็มจอแทนทั้งแอป code แสดง 503 แทน 500 หัวข้อบอกว่าส่วนไหนขัดข้อง

จอคอมพิวเตอร์ 1280 × 800 px
// 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>
  );
}