feat: 实现统一错误处理系统 (003-error-handling)

- 新增统一错误码定义和管理 (pkg/errors/codes.go)
- 新增全局错误处理器和中间件 (pkg/errors/handler.go, internal/middleware/error_handler.go)
- 新增错误上下文管理 (pkg/errors/context.go)
- 增强 Panic 恢复中间件 (internal/middleware/recover.go)
- 新增完整的单元测试和集成测试
- 新增功能文档 (docs/003-error-handling/)
- 新增功能规范 (specs/003-error-handling/)
- 更新 CLAUDE.md 和 README.md
This commit is contained in:
2025-11-15 12:17:44 +08:00
parent a371f1cd21
commit fb83c9a706
33 changed files with 7373 additions and 52 deletions

View File

@@ -117,6 +117,22 @@
- [ ] Body truncation indicates "... (truncated)" when over 50KB limit
- [ ] Access log includes all required fields: method, path, query, status, duration_ms, request_id, ip, user_agent, user_id, request_body, response_body
**Error Handling Standards** (Constitution Principle X):
- [ ] All API error responses use unified JSON format (via pkg/errors/ global ErrorHandler)
- [ ] Handler layer errors return error (not manual JSON responses)
- [ ] Business errors use pkg/errors.New() or pkg/errors.Wrap() with error codes
- [ ] All error codes defined in pkg/errors/codes.go
- [ ] All panics caught by Recover middleware and converted to 500 responses
- [ ] Error logs include complete request context (Request ID, path, method, params)
- [ ] 5xx server errors auto-sanitized (generic message to client, full error in logs)
- [ ] 4xx client errors may return specific business messages
- [ ] No panic in business code (except unrecoverable programming errors)
- [ ] No manual error response construction in Handler (c.Status().JSON())
- [ ] Error codes follow classification: 0=success, 1xxx=client (4xx), 2xxx=server (5xx)
- [ ] Recover middleware registered first in middleware chain
- [ ] Panic recovery logs complete stack trace
- [ ] Single request panic does not affect other requests
## Project Structure
### Documentation (this feature)