.claude/skills/error-response-system/SKILL.md
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.
npx skillsauth add vuthuonghai-steve/KLTN-By_Thuong_Hai-Steve error-response-systemInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
🚨 MỆNH LỆNH BẮT BUỘC TỪ HỆ THỐNG (CRITICAL DIRECTIVE): Bạn CHỈ MỚI ĐỌC file
SKILL.mdnày. Trí tuệ của bạn chưa được nạp đầy đủ. Hệ thống KHÔNG tự động nạp các file kiến thức khác trong thư mục. Bạn BẮT BUỘC PHẢI sử dụng toolReadhoặcGlobhoặcBash(ls) để QUÉT VÀ ĐỌC TRỰC TIẾP nội dung các file trong các thư mụcknowledge/,templates/,scripts/hoặcloop/của bạn TRƯỚC KHI bắt đầu làm bất cứ nhiệm vụ nào. Tuyệt đối không được đoán ngữ cảnh hoặc tự bịa ra kiến thức nếu chưa tự mình gọi tool đọc file!
Skill nay cung cap he thong chuan hoa error responses cho toan bo API cua Agent-skill. No giup:
[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]
Su dung khi error lien quan den HTTP protocol:
| Code | Su dung khi | |------|-------------| | 400 | Request sai format, thieu params | | 401 | Chua dang nhap, token het han | | 403 | Khong co quyen truy cap | | 404 | Resource khong ton tai | | 409 | Conflict (duplicate, da ton tai) | | 422 | Validation failed | | 429 | Rate limit exceeded | | 500 | Server error |
Su dung khi error lien quan den nghiep vu:
| Prefix | Domain | Vi du | |--------|--------|-------| | AUTH_ | Authentication | AUTH_001: Email da ton tai | | ORD_ | Orders | ORD_001: Don hang da huy | | PAY_ | Payment | PAY_001: So du khong du | | PTS_ | Points | PTS_001: Diem khong du | | VCH_ | Voucher | VCH_001: Voucher het han | | STR_ | Store | STR_001: Cua hang khong hoat dong | | PRD_ | Product | PRD_001: San pham het hang | | SYS_ | System | SYS_001: Service khong kha dung |
Su dung khi form/request validation that bai:
Doc file src/lib/errors/error-codes.json:
# Tim error code theo keyword
grep -r "AUTH_" src/lib/errors/error-codes.json
Neu chua co error code phu hop, them vao file error-codes.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_NNNSau khi them error code moi, chay:
python scripts/generate-types.py
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')
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'
})
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))
}
import { createError } from '@/lib/errors'
return createError('CUSTOM_ERROR', 'Custom message', {
status: 418,
details: 'Technical details',
suggestion: 'How to fix'
})
{
"success": true,
"data": { ... },
"message": "Thanh cong"
}
{
"success": false,
"error": {
"code": "AUTH_002",
"message": "Email da duoc su dung",
"details": "Email: [email protected] 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"
}
}
Truoc (cu - van hoat dong):
return errorResponse('Email da ton tai', 409)
Sau (moi - khuyen nghi):
return businessError('AUTH', 'AUTH_002')
Code cu van hoat dong binh thuong. api-response.ts da re-export tu ./errors:
// File: src/lib/api-response.ts
export {
createError,
httpError,
validationError,
businessError,
withErrorHandler,
} from './errors'
| File | Muc dich |
|------|----------|
| assets/error-codes-template.json | Template error codes |
| scripts/validate-error-codes.py | Validate JSON format |
| scripts/generate-types.py | Generate TypeScript types |
| references/implementation-guide.md | Patterns chi tiet |
| references/migration-guide.md | Huong dan migration |
| File | Muc dich |
|------|----------|
| src/lib/errors/index.ts | Re-export module |
| src/lib/errors/types.ts | TypeScript interfaces |
| src/lib/errors/helpers.ts | Helper functions |
| src/lib/errors/handler.ts | Global error handler |
| src/lib/errors/error-codes.json | Error codes config |
// 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
}
// 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
}
Skill nay duoc kich hoat khi:
scripts/validate-error-codes.pyscripts/generate-types.pytools
Automates end-to-end drawing of UI screens in Pencil canvas from module spec files. Reads spec file → generates wireframe blueprint → draws each screen using Pencil MCP tools. Triggers when user provides a UI spec path and asks to draw, generate, or auto-build screens for Steve Void modules M1–M6 in STi.pen.
testing
Extracts UI Screen Specs by analyzing Schema and Diagrams. Use when you need to bridge database logic and flow diagrams into intermediate UI component specifications for a given module. Trigger when user says "analyze UI for module X", "generate ui spec", "phân tích UI module", or invokes "ui-architecture-analyst --module M[X]".
development
Giải thích lỗi TypeScript một cách dễ hiểu bằng tiếng Việt. Sử dụng khi gặp lỗi type, generic, inference, hoặc bất kỳ lỗi TS nào cần được giải thích rõ ràng.
development
Skill phan tach yeu cau/tinh nang thanh cac phase, task va subtask cu the. Tao bo tai lieu planning clean, khong chua code mau, tap trung mo ta logic va nghiep vu. Su dung khi: (1) nhan yeu cau tinh nang moi can lap ke hoach, (2) co tai lieu nghien cuu can chuyen thanh task plan, (3) nguoi dung yeu cau phan tach cong viec, (4) can tao roadmap trien khai cho du an/tinh nang. Trigger: /task-planner, /plan-tasks, "phan tach task", "lap ke hoach", "tao plan", "chia phase".