Communitygithub.com

YepAPI/skills

錯誤安全機制 — 安全的錯誤回應、關聯 ID,且在生產環境中不洩漏堆疊追蹤。

skills 是什麼?

這個技能專注於錯誤安全,確保應用程式在生產環境中不會將敏感資訊(如堆疊追蹤、資料庫錯誤或檔案路徑)洩漏給客戶端。它建議透過唯一的錯誤 ID 傳回通用錯誤訊息,並在伺服器端記錄完整的錯誤細節以便除錯。該技能支援 Express 和 Next.js 等平台,並提供了中間件與錯誤邊界(Error Boundary)的實作範例,可根據開發或生產環境調整錯誤詳細程度。使用此技能,開發者可以一致地處理 HTTP 狀態碼(如 400、401、403、404、500),同時避免常見陷阱,例如直接回傳 `err.message`、在生產回應中包含堆疊追蹤,或未使用關聯 ID 記錄錯誤。總體而言,它透過結構化的錯誤處理模式,提升了安全性與可除錯性。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/error-security

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

錯誤安全

規則

  • 絕不在生產環境中將堆疊追蹤、資料庫錯誤或檔案路徑洩漏給客戶端
  • 使用唯一的錯誤 ID 回傳通用錯誤訊息:{ error: "Something went wrong", errorId: "abc123" }
  • 在伺服器端使用相同的關聯 ID 記錄完整的錯誤細節,以便除錯
  • 使用不同的錯誤詳細程度:開發環境(完整堆疊追蹤) vs 生產環境(安全訊息 + ID)
  • 使用全域錯誤處理器捕獲所有未處理的錯誤 — 絕不讓原始錯誤到達客戶端
  • 在轉發給客戶端之前,對來自第三方服務的錯誤訊息進行清理
  • 回傳適當的 HTTP 狀態碼:400(錯誤輸入)、401(未驗證)、403(禁止)、404(找不到)、500(伺服器錯誤)
// Express 錯誤處理中介軟體
import { randomUUID } from "node:crypto";

app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
  const errorId = randomUUID();

  // 在伺服器端記錄完整細節
  console.error({ errorId, message: err.message, stack: err.stack, url: req.url });

  // 回傳安全的回應給客戶端
  const statusCode = (err as any).statusCode || 500;
  res.status(statusCode).json({
    error: statusCode >= 500 ? "Internal server error" : err.message,
    errorId,
    ...(process.env.NODE_ENV === "development" && { stack: err.stack }),
  });
});
// Next.js 錯誤邊界 — 安全的生產環境錯誤

export default function GlobalError({ error, reset }) {
  return (
    <div>
      <h2>Something went wrong</h2>
      <p>Error ID: {error.digest}</p>
      <button onClick={reset}>Try again</button>
    </div>
  );
}

避免

  • 直接回傳 err.message — 資料庫錯誤會洩漏資料表名稱、欄位名稱和查詢結構
  • 在生產環境回應中包含堆疊追蹤 — 它們會洩漏檔案路徑、依賴項和內部架構
  • 對所有情況都使用通用的 500 錯誤 — 應使用適當的狀態碼,以便客戶端能正確處理錯誤
  • 在沒有關聯 ID 的情況下記錄錯誤 — 這會讓生產環境的除錯幾乎不可能進行
  • 靜默地吞沒錯誤 — 即使回應是通用的,也務必記錄錯誤

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相關技能