AgentSkillsCN

error-response-system

为API统一错误响应格式的技能。当您需要:(1) 新建错误响应;(2) 为特定功能添加错误码;(3) 调试API错误;(4) 将旧有格式迁移至全新格式时,可启用此技能。当用户希望统一错误响应的格式,或希望建立全新的错误处理体系时,此技能尤为实用。

SKILL.md
--- frontmatter
name: error-response-system
description: Skill chuan hoa Error Response cho API. Su dung khi can (1) tao error response moi, (2) them error code cho feature, (3) debug loi API, (4) migration tu format cu sang format moi. Skill nay nen duoc su dung khi nguoi dung can thong nhat error response format hoac tao he thong error handling moi.

Error Response System

Overview

Skill nay cung cap he thong chuan hoa error responses cho toan bo API cua Agent-skill. No giup:

  1. Developer: Debug nhanh hon voi error codes cu the
  2. Frontend: Hien thi message va suggestion cho user
  3. Team: Thong nhat format error response toan project
  4. Maintenance: De dang them error codes moi qua JSON config

Khi Nao Su Dung Skill Nay

1. Tao Error Response Moi

  • Khi can tra ve error tu API endpoint
  • Khi can validation error voi field cu the
  • Khi can business logic error (AUTH, ORD, PAY, etc.)

2. Them Error Code Cho Feature

  • Khi phat trien feature moi can error handling
  • Khi can error code cho nghiep vu cu the
  • Khi can thong bao loi co y nghia cho user

3. Debug Loi API

  • Khi can hieu y nghia cua error code
  • Khi can tim nguyen nhan loi tu code
  • Khi can goi y khac phuc loi

4. Migration Tu Format Cu

  • Khi can chuyen doi errorResponse() -> businessError()
  • Khi can them error code cho API hien tai
  • Khi can thong nhat format toan project

Workflow Tong Quan

code
[Trigger: Can error handling]
         |
         v
[PHASE 1: Xac Dinh Error Type]
- HTTP Error (400, 401, 404, 500)?
- Business Error (AUTH, ORD, PAY)?
- Validation Error?
         |
         v
[PHASE 2: Chon/Tao Error Code]
- Tim trong error-codes.json
- Tao code moi neu chua co
- Dung dung prefix (AUTH_, ORD_, PAY_)
         |
         v
[PHASE 3: Su Dung Helper]
- httpError() cho HTTP errors
- businessError() cho business logic
- validationError() cho form validation
         |
         v
[Output: Structured Error Response]

PHASE 1: Xac Dinh Error Type

HTTP Errors (4xx, 5xx)

Su dung khi error lien quan den HTTP protocol:

CodeSu dung khi
400Request sai format, thieu params
401Chua dang nhap, token het han
403Khong co quyen truy cap
404Resource khong ton tai
409Conflict (duplicate, da ton tai)
422Validation failed
429Rate limit exceeded
500Server error

Business Errors

Su dung khi error lien quan den nghiep vu:

PrefixDomainVi du
AUTH_AuthenticationAUTH_001: Email da ton tai
ORD_OrdersORD_001: Don hang da huy
PAY_PaymentPAY_001: So du khong du
PTS_PointsPTS_001: Diem khong du
VCH_VoucherVCH_001: Voucher het han
STR_StoreSTR_001: Cua hang khong hoat dong
PRD_ProductPRD_001: San pham het hang
SYS_SystemSYS_001: Service khong kha dung

Validation Errors

Su dung khi form/request validation that bai:

  • Field-level validation (email, password, etc.)
  • Business rules validation
  • Format validation (phone, date, etc.)

PHASE 2: Chon/Tao Error Code

Buoc 2.1: Tim Error Code Co San

Doc file src/lib/errors/error-codes.json:

bash
# Tim error code theo keyword
grep -r "AUTH_" src/lib/errors/error-codes.json

Buoc 2.2: Tao Error Code Moi (Neu Can)

Neu chua co error code phu hop, them vao file error-codes.json:

json
{
  "BUSINESS_ERRORS": {
    "AUTH": {
      "AUTH_010": {
        "code": "AUTH_010",
        "status": 400,
        "message": "Thong bao cho user",
        "details": "Chi tiet ky thuat cho developer",
        "suggestion": "Goi y cach khac phuc"
      }
    }
  }
}

Quy tac dat ten:

  • Prefix: 3 ky tu viet hoa (AUTH, ORD, PAY, etc.)
  • Number: 3 chu so (001, 002, 010, etc.)
  • Format: PREFIX_NNN

Buoc 2.3: Generate Types

Sau khi them error code moi, chay:

bash
python scripts/generate-types.py

PHASE 3: Su Dung Helper Functions

HTTP Errors

typescript
import { httpError } from '@/lib/errors'

// 400 Bad Request
return httpError('BAD_REQUEST')

// 401 Unauthorized
return httpError('UNAUTHORIZED')

// 404 Not Found
return httpError('NOT_FOUND')

// Custom message
return httpError('NOT_FOUND', 'San pham khong ton tai')

Business Errors

typescript
import { businessError } from '@/lib/errors'

// Voi error code
return businessError('AUTH', 'AUTH_002')

// Voi custom message
return businessError('ORD', 'ORD_001', {
  details: 'Order ID: 12345'
})

Validation Errors

typescript
import { validationError, formatZodErrors } from '@/lib/errors'

// Single field
return validationError('email', 'Email khong hop le')

// Multiple fields (tu Zod)
const result = schema.safeParse(data)
if (!result.success) {
  return validationError(formatZodErrors(result.error))
}

Custom Errors

typescript
import { createError } from '@/lib/errors'

return createError('CUSTOM_ERROR', 'Custom message', {
  status: 418,
  details: 'Technical details',
  suggestion: 'How to fix'
})

Response Format

Success Response (Giu Nguyen)

json
{
  "success": true,
  "data": { ... },
  "message": "Thanh cong"
}

Error Response (Moi)

json
{
  "success": false,
  "error": {
    "code": "AUTH_002",
    "message": "Email da duoc su dung",
    "details": "Email: user@example.com da ton tai trong he thong",
    "field": "email",
    "suggestion": "Su dung email khac hoac dang nhap",
    "timestamp": "2026-01-16T10:30:00.000Z",
    "requestId": "req_abc123"
  }
}

Migration Guide

Tu Format Cu Sang Format Moi

Truoc (cu - van hoat dong):

typescript
return errorResponse('Email da ton tai', 409)

Sau (moi - khuyen nghi):

typescript
return businessError('AUTH', 'AUTH_002')

Backward Compatibility

Code cu van hoat dong binh thuong. api-response.ts da re-export tu ./errors:

typescript
// File: src/lib/api-response.ts
export {
  createError,
  httpError,
  validationError,
  businessError,
  withErrorHandler,
} from './errors'

Files Quan Trong

Skill Resources

FileMuc dich
assets/error-codes-template.jsonTemplate error codes
scripts/validate-error-codes.pyValidate JSON format
scripts/generate-types.pyGenerate TypeScript types
references/implementation-guide.mdPatterns chi tiet
references/migration-guide.mdHuong dan migration

Implementation Files (Khi Deploy)

FileMuc dich
src/lib/errors/index.tsRe-export module
src/lib/errors/types.tsTypeScript interfaces
src/lib/errors/helpers.tsHelper functions
src/lib/errors/handler.tsGlobal error handler
src/lib/errors/error-codes.jsonError codes config

Vi Du Cu The

1. Dang Ky Nguoi Dung

typescript
// src/app/api/v1/auth/register/route.ts
import { businessError, validationError } from '@/lib/errors'

export async function POST(req: Request) {
  const data = await req.json()

  // Validation error
  const result = registerSchema.safeParse(data)
  if (!result.success) {
    return validationError(formatZodErrors(result.error))
  }

  // Check email exists
  const existing = await payload.find({
    collection: 'users',
    where: { email: { equals: data.email } }
  })

  if (existing.docs.length > 0) {
    return businessError('AUTH', 'AUTH_002')
  }

  // ... create user
}

2. Tao Don Hang

typescript
// src/app/api/v1/orders/route.ts
import { businessError, httpError } from '@/lib/errors'

export async function POST(req: Request) {
  const user = await getCurrentUser(req)
  if (!user) {
    return httpError('UNAUTHORIZED')
  }

  const cart = await getCart(user.id)
  if (cart.items.length === 0) {
    return businessError('ORD', 'ORD_002')
  }

  // Check stock
  for (const item of cart.items) {
    const product = await getProduct(item.productId)
    if (product.stock < item.quantity) {
      return businessError('PRD', 'PRD_001', {
        details: `San pham: ${product.name}`
      })
    }
  }

  // ... create order
}

Trigger Phrases

Skill nay duoc kich hoat khi:

Tao Error Response

  • "tao error response"
  • "tra ve loi tu API"
  • "error handling cho endpoint"

Them Error Code

  • "them error code"
  • "tao error code moi"
  • "error cho feature X"

Debug Loi

  • "error code X nghia la gi"
  • "debug loi API"
  • "hieu error response"

Migration

  • "migration error response"
  • "chuyen doi sang format moi"
  • "thong nhat error format"

Luu Y Quan Trong

  1. LUON dung error code tu error-codes.json - Khong hardcode message
  2. GIU nguyen code cu - Format moi la bo sung, khong thay the
  3. Validate truoc khi commit - Chay scripts/validate-error-codes.py
  4. Generate types sau khi them code - Chay scripts/generate-types.py
  5. Message bang tieng Viet - De user hieu
  6. Suggestion co ich - Giup user biet cach khac phuc