Skip to content
error-code-guide logo

錯誤碼指南

error-code-guide

設計一致的錯誤碼,遵循 PREFIX_CATEGORY_NUMBER 格式。 使用時機:定義錯誤碼、建立錯誤處理、設計 API。 關鍵字:error code, error handling, error format, API errors, 錯誤碼, 錯誤處理。

SKILL.md

Full skill instructions

錯誤碼指南

語言: English | 繁體中文

版本: 1.0.0 最後更新: 2025-12-30 適用範圍: Claude Code Skills


目的

此技能幫助設計一致的錯誤碼,遵循標準格式,實現更好的除錯、監控和使用者體驗。

快速參考

錯誤碼格式

<前綴>_<類別>_<編號>
元素說明範例
前綴 (PREFIX)應用/服務識別碼AUTH, PAY, USR
類別 (CATEGORY)錯誤類別VAL, SYS, BIZ
編號 (NUMBER)唯一數字識別碼001, 100, 404

範例

AUTH_VAL_001    → 認證驗證錯誤
PAY_SYS_503     → 付款系統無法使用
USR_BIZ_100     → 使用者商業規則違規
API_NET_408     → API 網路逾時

錯誤類別

類別全名說明HTTP 狀態碼
VALValidation客戶端輸入驗證失敗400
BIZBusiness商業規則違規422
SYSSystem內部系統錯誤500
NETNetwork通訊錯誤502/​503/​504
AUTHAuth安全相關錯誤401/​403

類別編號範圍

範圍說明範例
*_VAL_001-099欄位驗證缺少必填欄位
*_VAL_100-199格式驗證電子郵件格式無效
*_VAL_200-299約束驗證密碼太短
*_BIZ_001-099狀態違規訂單已取消
*_BIZ_100-199規則違規超過 30 天無法退貨
*_BIZ_200-299限制違規超過每日限制
*_AUTH_001-099認證帳號密碼錯誤
*_AUTH_100-199授權權限不足
*_AUTH_200-299Token/​SessionToken 已過期

HTTP 狀態碼對應

類別HTTP 狀態碼說明
VAL400Bad Request
BIZ422Unprocessable Entity
AUTH (001-099)401Unauthorized
AUTH (100-199)403Forbidden
SYS500Internal Server Error
NET502/​503/​504Gateway errors

詳細指南

完整標準請參考:

AI 優化格式(節省 Token)

AI 助手可使用 YAML 格式檔案以減少 Token 使用量:

  • 基礎標準:ai/​standards/​error-codes.ai.yaml

錯誤回應格式

單一錯誤

{
  "success": false,
  "error": {
    "code": "AUTH_VAL_001",
    "message": "電子郵件為必填欄位",
    "field": "email",
    "requestId": "req_abc123"
  }
}

多個錯誤

{
  "success": false,
  "errors": [
    {
      "code": "AUTH_VAL_001",
      "message": "電子郵件為必填欄位",
      "field": "email"
    },
    {
      "code": "AUTH_VAL_201",
      "message": "密碼至少需要 8 個字元",
      "field": "password"
    }
  ],
  "requestId": "req_abc123"
}

內部錯誤物件

interface ApplicationError {
  // 核心欄位
  code: string;          // "AUTH_VAL_001"
  message: string;       // 技術訊息(用於日誌)

  // 使用者介面
  userMessage: string;   // 本地化使用者訊息
  userMessageKey: string; // i18n 鍵值: "error.auth.val.001"

  // 上下文
  field?: string;        // 相關欄位: "email"
  details?: object;      // 附加資訊

  // 除錯
  timestamp: string;     // ISO 8601
  requestId: string;     // 關聯 ID
}

國際化 (i18n)

訊息鍵值格式

error.<前綴>.<類別>.<編號>

翻譯檔案範例

# en.yaml
error:
  auth:
    val:
      001: "Email is required"
      101: "Invalid email format"
    auth:
      001: "Invalid credentials"

# zh-TW.yaml
error:
  auth:
    val:
      001: "電子郵件為必填欄位"
      101: "電子郵件格式無效"
    auth:
      001: "帳號或密碼錯誤"

範例

✅ 良好的錯誤碼

AUTH_VAL_001  // 缺少必填欄位: email
AUTH_VAL_101  // 電子郵件格式無效
ORDER_BIZ_001 // 訂單已取消
ORDER_BIZ_201 // 超過每日購買限制
DB_SYS_001    // 資料庫查詢失敗
SEC_AUTH_001  // 帳號密碼錯誤
SEC_AUTH_201  // Token 已過期

❌ 不良的錯誤碼

ERR_001       // 太模糊,沒有前綴或類別
INVALID       // 不具描述性
error         // 不是錯誤碼
AUTH_ERROR    // 缺少編號

檢查清單

  • 每個錯誤有唯一代碼
  • 類別符合錯誤類型
  • 使用者訊息已本地化
  • HTTP 狀態碼正確
  • 錯誤已記錄文件
  • 代碼已加入註冊表

設定偵測

此技能支援專案特定設定。

偵測順序

  1. 檢查程式碼庫中現有的錯誤碼模式
  2. 檢查 CONTRIBUTING.md 中的錯誤碼指南
  3. 若無找到,預設使用 PREFIX_CATEGORY_NUMBER 格式

首次設定

若未找到錯誤碼標準:

  1. 建議:「此專案尚未設定錯誤碼標準。您要建立錯誤碼註冊表嗎?」
  2. 建議建立 errors/​registry.ts:
export const ErrorCodes = {
  AUTH_VAL_001: {
    code: 'AUTH_VAL_001',
    httpStatus: 400,
    messageKey: 'error.auth.val.001',
    description: '電子郵件欄位為必填',
  },
  // ... 更多錯誤碼
} as const;

相關標準


版本歷史

版本日期變更
1.0.02025-12-30初始發布

授權

此技能採用 CC BY 4.0 授權。

來源: universal-dev-standards

More Other skills

Delegate independent tasks to specialized agents working concurrently with isolated context.

295.4K 206.3K
View

Applies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.

179.7K 100.1K
View

Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with colors/fonts that you can apply to any artifact that has been creating, or can generate a new theme on-the-fly.

179.7K 89.5K
View

A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill whenever asked to write some sort of internal communications (status reports, leadership updates, 3P updates, company newsletters, FAQs, incident reports, project updates, etc.).

179.7K 76.1K
View

Fundamental design principles for writing maintainable, testable Python code.

40.2K 21.9K
View

Modern responsive layouts using container queries, fluid typography, CSS Grid, and mobile-first strategies.

40.2K 20K
View

Generate Playwright tests from scenarios using interactive browser exploration and validation.

39.7K 18.2K
View

Modern React state management with Redux Toolkit, Zustand, Jotai, and React Query for every state category.

40.2K 14.7K
View

GDPR-compliant data handling with consent management, data subject rights, and privacy controls.

40.2K 14.3K
View

Python error handling patterns including input validation, exception hierarchies, and partial failure handling. Use when implementing validation logic, designing exception strategies, handling batch processing failures, or building robust APIs.

40.2K 13.5K
View

Detailed multi-file refactor planning with safety checks and rollback guidance.

39.7K 13K
View