create-dp-koa 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (235) hide show
  1. package/README.md +50 -0
  2. package/index.mjs +97 -0
  3. package/package.json +33 -0
  4. package/template/.env.development +9 -0
  5. package/template/.env.production +12 -0
  6. package/template/.github/workflows/ci-cd.yml +182 -0
  7. package/template/.trae/documents/controller_development_plan.md +386 -0
  8. package/template/.trae/skills/00-backend-core.skill.md +50 -0
  9. package/template/.trae/skills/01-backend-skill-router.skill.md +55 -0
  10. package/template/.trae/skills/10-backend-api.skill.md +54 -0
  11. package/template/.trae/skills/11-backend-controller-recipes.skill.md +107 -0
  12. package/template/.trae/skills/20-backend-repository.skill.md +25 -0
  13. package/template/.trae/skills/21-backend-service.skill.md +135 -0
  14. package/template/.trae/skills/25-backend-comments-and-doc.skill.md +97 -0
  15. package/template/.trae/skills/30-backend-validation.skill.md +320 -0
  16. package/template/.trae/skills/40-backend-error-logging.skill.md +21 -0
  17. package/template/.trae/skills/50-backend-bootstrap-lifecycle.skill.md +90 -0
  18. package/template/.trae/skills/60-backend-router-registration.skill.md +71 -0
  19. package/template/.trae/skills/70-backend-middleware.skill.md +98 -0
  20. package/template/.trae/skills/80-backend-utils-and-libs.skill.md +90 -0
  21. package/template/.trae/skills/85-backend-plugins.rule.md +64 -0
  22. package/template/.trae/skills/90-backend-testing.skill.md +29 -0
  23. package/template/.trae/skills/README.md +49 -0
  24. package/template/.vscode/launch.json +38 -0
  25. package/template/.vscode/settings.json +1 -0
  26. package/template/Dockerfile +36 -0
  27. package/template/README.md +229 -0
  28. package/template/docker-compose.yml +135 -0
  29. package/template/docs/API_DOCUMENTATION.md +837 -0
  30. package/template/docs/ARCHITECTURE_REFACTOR.md +109 -0
  31. package/template/docs/CACHE_MIGRATION_GUIDE.md +142 -0
  32. package/template/docs/DEPLOYMENT_GUIDE.md +1062 -0
  33. package/template/docs/DEVELOPMENT_GUIDE.md +1097 -0
  34. package/template/docs/DOCUMENTATION_CLEANUP_REPORT.md +166 -0
  35. package/template/docs/DOCUMENTATION_COMPLETION_REPORT.md +223 -0
  36. package/template/docs/DOCUMENTATION_INDEX.md +294 -0
  37. package/template/docs/DOCUMENTATION_STRUCTURE.md +221 -0
  38. package/template/docs/ENTERPRISE_ANNOTATION_SYSTEM_GUIDE.md +2069 -0
  39. package/template/docs/ENTERPRISE_DATABASE_ARCHITECTURE.md +318 -0
  40. package/template/docs/ENTERPRISE_DEPLOYMENT_GUIDE.md +547 -0
  41. package/template/docs/ENTERPRISE_ERROR_HANDLING_GUIDE.md +357 -0
  42. package/template/docs/ENTERPRISE_LOGGING_SYSTEM_GUIDE.md +494 -0
  43. package/template/docs/ENVIRONMENT_CONFIG_EXAMPLE.md +69 -0
  44. package/template/docs/FINAL_IMPLEMENTATION_SUMMARY.md +206 -0
  45. package/template/docs/HEALTH_CHECK_ROUTE_FIX.md +134 -0
  46. package/template/docs/IMPLEMENTATION_CHECKLIST.md +204 -0
  47. package/template/docs/INSTALLATION_GUIDE.md +611 -0
  48. package/template/docs/INTERCEPTOR_TESTING_REPORT.md +226 -0
  49. package/template/docs/INTERCEPTOR_TESTING_SCRIPTS.md +143 -0
  50. package/template/docs/LOGGING_OPTIMIZATION_GUIDE.md +126 -0
  51. package/template/docs/MEMORY_DATABASE_GUIDE.md +212 -0
  52. package/template/docs/NEW_ROUTER_INTEGRATION_GUIDE.md +345 -0
  53. package/template/docs/NEW_ROUTER_INTEGRATION_SUMMARY.md +259 -0
  54. package/template/docs/NEW_ROUTER_USAGE_GUIDE.md +364 -0
  55. package/template/docs/QUICK_START.md +268 -0
  56. package/template/docs/ROUTE_SLASH_COMPATIBILITY_FIX.md +191 -0
  57. package/template/docs/SERVICE_INTERCEPTOR_GUIDE.md +243 -0
  58. package/template/docs/SERVICE_LAYER_INDEX.md +205 -0
  59. package/template/docs/SERVICE_PATTERN_GUIDE.md +270 -0
  60. package/template/docs/SERVICE_RETURN_VALUE_SPECIFICATION.md +466 -0
  61. package/template/docs/SWAGGER_DEBUG_MODE_GUIDE.md +80 -0
  62. package/template/docs/SWAGGER_INTEGRATION_GUIDE.md +416 -0
  63. package/template/docs/TRANSACTION_MANAGER_USAGE.md +360 -0
  64. package/template/docs/TROUBLESHOOTING.md +869 -0
  65. package/template/env.production.example +62 -0
  66. package/template/jest.config.js +34 -0
  67. package/template/package-lock.json +13240 -0
  68. package/template/package.json +119 -0
  69. package/template/patches/typeorm+0.3.25.patch +22 -0
  70. package/template/scripts/sync-template.mjs +84 -0
  71. package/template/scripts/test-annotation-system.sh +48 -0
  72. package/template/scripts/test-core-functionality.sh +28 -0
  73. package/template/src/annotations/decorators/ConfigManagement.ts +9 -0
  74. package/template/src/annotations/decorators/DistributedTracing.ts +9 -0
  75. package/template/src/annotations/decorators/EnterprisePerformance.ts +9 -0
  76. package/template/src/annotations/decorators/PerformanceMonitor.ts +32 -0
  77. package/template/src/annotations/decorators/SecurityAudit.ts +9 -0
  78. package/template/src/annotations/index.ts +50 -0
  79. package/template/src/annotations/processors/ConfigManagementProcessor.ts +369 -0
  80. package/template/src/annotations/processors/DistributedTracingProcessor.ts +288 -0
  81. package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +189 -0
  82. package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +101 -0
  83. package/template/src/annotations/processors/SecurityAuditProcessor.ts +345 -0
  84. package/template/src/annotations/processors/SwaggerProcessor.ts +612 -0
  85. package/template/src/annotations/processors/index.ts +10 -0
  86. package/template/src/app.ts +123 -0
  87. package/template/src/controllers/base.controller.ts +41 -0
  88. package/template/src/controllers/cacheManagement.controller.ts +131 -0
  89. package/template/src/controllers/captcha.controller.ts +57 -0
  90. package/template/src/controllers/demo/AnnotationDemoController.ts +118 -0
  91. package/template/src/controllers/example/EnterpriseExampleController.ts +297 -0
  92. package/template/src/controllers/example/ExampleController.ts +110 -0
  93. package/template/src/controllers/example/NewAnnotationExampleController.ts +159 -0
  94. package/template/src/controllers/example/SwaggerExampleController.ts +205 -0
  95. package/template/src/controllers/example/TransactionExample.controller.ts +336 -0
  96. package/template/src/controllers/health.controller.ts +235 -0
  97. package/template/src/controllers/home/register.controller.ts +58 -0
  98. package/template/src/controllers/home/ytGoods.controller.ts +92 -0
  99. package/template/src/controllers/home/ytShop.controller.ts +135 -0
  100. package/template/src/controllers/home/ytUser.controller.ts +89 -0
  101. package/template/src/controllers/logManagement.controller.ts +396 -0
  102. package/template/src/controllers/public/emailSend.controller.ts +65 -0
  103. package/template/src/controllers/public/ytUserAuth.controller.ts +174 -0
  104. package/template/src/controllers/testData.controller.ts +253 -0
  105. package/template/src/dto/controller/example/NewAnnotationExampleController.dto.ts +73 -0
  106. package/template/src/dto/controller/home/emailSend.controller.dto.ts +40 -0
  107. package/template/src/dto/controller/home/register.controller.dto.ts +45 -0
  108. package/template/src/dto/controller/home/ytGoods.controller.dto.ts +55 -0
  109. package/template/src/dto/controller/home/ytShop.controller.dto.ts +69 -0
  110. package/template/src/dto/controller/home/ytUser.controller.dto.ts +44 -0
  111. package/template/src/dto/controller/public/ytUserAuth.controller.dto.ts +63 -0
  112. package/template/src/dto/goods.dto.ts +212 -0
  113. package/template/src/dto/service/ytService.dto.ts +13 -0
  114. package/template/src/dto/user.dto.ts +177 -0
  115. package/template/src/entity/base.entity.ts +13 -0
  116. package/template/src/entity/columnTypes.ts +13 -0
  117. package/template/src/entity/goodsImagesUnlockKey.entity.ts +33 -0
  118. package/template/src/entity/goodsUnlocker.entity.ts +34 -0
  119. package/template/src/entity/index.ts +15 -0
  120. package/template/src/entity/shop.entity.ts +52 -0
  121. package/template/src/entity/shopUser.entity.ts +41 -0
  122. package/template/src/entity/ytGoods.entity.ts +94 -0
  123. package/template/src/entity/ytUser.entity.ts +96 -0
  124. package/template/src/examples/InterceptorExampleRunner.ts +284 -0
  125. package/template/src/examples/ServiceInterceptorExample.ts +214 -0
  126. package/template/src/examples/SwaggerProcessorExample.ts +169 -0
  127. package/template/src/examples/TransactionManagerDemo.ts +377 -0
  128. package/template/src/examples/cacheExamples.ts +155 -0
  129. package/template/src/framework/decorator/controller.ts +311 -0
  130. package/template/src/framework/decorator/processor/AnnotationDecorators.ts +100 -0
  131. package/template/src/framework/decorator/processor/AnnotationProcessor.ts +156 -0
  132. package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +45 -0
  133. package/template/src/framework/decorator/processor/AnnotationRegistry.ts +117 -0
  134. package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +95 -0
  135. package/template/src/framework/decorator/processor/ProcessorManager.ts +76 -0
  136. package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +126 -0
  137. package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +207 -0
  138. package/template/src/framework/decorator/refactored/DecoratorFactory.ts +99 -0
  139. package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +125 -0
  140. package/template/src/framework/decorator/refactored/DecoratorValidator.ts +128 -0
  141. package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +139 -0
  142. package/template/src/framework/decorator/refactored/index.ts +98 -0
  143. package/template/src/framework/decorator/swagger.ts +150 -0
  144. package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +375 -0
  145. package/template/src/framework/interceptors/ServiceCallInterceptor.ts +348 -0
  146. package/template/src/framework/interceptors/index.ts +19 -0
  147. package/template/src/framework/plugins/registry.ts +63 -0
  148. package/template/src/framework/plugins/types.ts +15 -0
  149. package/template/src/framework/types/ServiceResult.ts +151 -0
  150. package/template/src/framework/types/index.ts +16 -0
  151. package/template/src/framework/utils/CacheManager.ts +430 -0
  152. package/template/src/framework/utils/CacheService.ts +248 -0
  153. package/template/src/framework/utils/DtoValidator.ts +164 -0
  154. package/template/src/framework/utils/MigrationHelper.ts +179 -0
  155. package/template/src/framework/utils/MigrationManager.ts +256 -0
  156. package/template/src/framework/utils/NewRouter.ts +207 -0
  157. package/template/src/framework/utils/TransactionManager.ts +172 -0
  158. package/template/src/framework/utils/bootstrap.ts +445 -0
  159. package/template/src/framework/utils/cache.ts +269 -0
  160. package/template/src/framework/utils/databaseConfig.ts +148 -0
  161. package/template/src/framework/utils/db.ts +39 -0
  162. package/template/src/framework/utils/dbMonitor.ts +106 -0
  163. package/template/src/framework/utils/dynamicSwagger.ts +410 -0
  164. package/template/src/framework/utils/function.ts +61 -0
  165. package/template/src/framework/utils/gracefulShutdown.ts +131 -0
  166. package/template/src/framework/utils/logger.ts +388 -0
  167. package/template/src/framework/utils/metrics.ts +182 -0
  168. package/template/src/framework/utils/router.ts +417 -0
  169. package/template/src/framework/utils/swagger.ts +184 -0
  170. package/template/src/framework/utils/testDb.ts +19 -0
  171. package/template/src/framework/utils/token.ts +23 -0
  172. package/template/src/framework/utils/transform.ts +17 -0
  173. package/template/src/libs/aokEmailSender.ts +42 -0
  174. package/template/src/libs/captcha.ts +37 -0
  175. package/template/src/libs/cos.ts +45 -0
  176. package/template/src/libs/mCache.ts +7 -0
  177. package/template/src/libs/serviceValidate.ts +3 -0
  178. package/template/src/libs/tecentSms.ts +51 -0
  179. package/template/src/middlewares/a.middleware.ts +6 -0
  180. package/template/src/middlewares/error.middleware.ts +14 -0
  181. package/template/src/middlewares/logging.middleware.ts +187 -0
  182. package/template/src/middlewares/static.middleware.ts +79 -0
  183. package/template/src/middlewares/swagger.middleware.ts +70 -0
  184. package/template/src/middlewares/token.middleware.ts +32 -0
  185. package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +172 -0
  186. package/template/src/migrations/index.ts +6 -0
  187. package/template/src/plugins/weboffice/core/context.ts +47 -0
  188. package/template/src/plugins/weboffice/core/errors.ts +51 -0
  189. package/template/src/plugins/weboffice/core/types.ts +63 -0
  190. package/template/src/plugins/weboffice/core/utils.ts +7 -0
  191. package/template/src/plugins/weboffice/entities/index.ts +3 -0
  192. package/template/src/plugins/weboffice/entities/webofficeFile.entity.ts +28 -0
  193. package/template/src/plugins/weboffice/entities/webofficeFileVersion.entity.ts +29 -0
  194. package/template/src/plugins/weboffice/http/routes.ts +179 -0
  195. package/template/src/plugins/weboffice/index.ts +23 -0
  196. package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +274 -0
  197. package/template/src/repository/UserRepository.ts +122 -0
  198. package/template/src/repository/base/BaseRepository.ts +124 -0
  199. package/template/src/repository/interfaces/IBaseRepository.ts +67 -0
  200. package/template/src/routers/index.ts +49 -0
  201. package/template/src/service/base.service.ts +116 -0
  202. package/template/src/service/paramValidateTest.service.ts +139 -0
  203. package/template/src/service/ytGoods.service.ts +42 -0
  204. package/template/src/service/ytShop.service.ts +90 -0
  205. package/template/src/service/ytUser.service.ts +451 -0
  206. package/template/src/test/swaggerParameterTest.ts +90 -0
  207. package/template/src/utils/testDataInitializer.ts +296 -0
  208. package/template/static/output.json +15203 -0
  209. package/template/test/controllers/controllers.test.ts +173 -0
  210. package/template/test/controllers/example/ExampleController.test.ts +222 -0
  211. package/template/test/controllers/example/NewAnnotationExampleController.test.ts +200 -0
  212. package/template/test/framework/TransactionManagerDemo.test.ts +363 -0
  213. package/template/test/framework/annotation/AnnotationDecorators.test.ts +222 -0
  214. package/template/test/framework/annotation/AnnotationExecutor.test.ts +246 -0
  215. package/template/test/framework/annotation/AnnotationProcessor.test.ts +179 -0
  216. package/template/test/framework/annotation/CustomProcessors.test.ts +313 -0
  217. package/template/test/framework/annotation/DefaultProcessors.test.ts +371 -0
  218. package/template/test/framework/annotation/NewRouter.test.ts +272 -0
  219. package/template/test/framework/annotation/ProcessorManager.test.ts +248 -0
  220. package/template/test/framework/annotation/setup.ts +26 -0
  221. package/template/test/framework/cache.test.ts +101 -0
  222. package/template/test/framework/databaseConfig.test.ts +142 -0
  223. package/template/test/integration/integration.test.ts +153 -0
  224. package/template/test/plugins/weboffice/http.routes.int.test.ts +61 -0
  225. package/template/test/service/business.test.ts +87 -0
  226. package/template/test/service/paramValidateTest.service.test.ts +184 -0
  227. package/template/test/service/ytUser.service.test.ts +566 -0
  228. package/template/test/setup.ts +20 -0
  229. package/template/test/setupAfterEnv.ts +14 -0
  230. package/template/test/utils/testHelpers.ts +220 -0
  231. package/template/test_output.txt +0 -0
  232. package/template/tsconfig.build.json +17 -0
  233. package/template/tsconfig.json +31 -0
  234. package/template/webpack.config.js +71 -0
  235. package/template/yarn.lock +7354 -0
@@ -0,0 +1,357 @@
1
+ # 企业级异常处理系统文档
2
+
3
+ ## 概述
4
+
5
+ 本文档描述了 dp-koa-framework 框架的企业级异常处理系统,该系统完全符合企业开发规范,提供了完整的错误分类、安全防护、监控告警和标准化响应功能。
6
+
7
+ ## 架构设计
8
+
9
+ ### 设计原则
10
+
11
+ 1. **分层架构**:框架层完全独立,不依赖业务层实现
12
+ 2. **安全优先**:敏感信息自动过滤,防止信息泄露
13
+ 3. **标准化**:统一的错误码和响应格式
14
+ 4. **可观测性**:完整的日志记录和监控告警
15
+ 5. **环境适配**:开发/生产环境差异化处理
16
+
17
+ ### 核心组件
18
+
19
+ ```mermaid
20
+ graph TD
21
+ A[异常发生] --> B[错误分类和标准化]
22
+ B --> C[安全信息过滤]
23
+ C --> D[结构化日志记录]
24
+ D --> E{严重程度检查}
25
+ E -->|high/critical| F[触发告警]
26
+ E -->|low/medium| G[记录日志]
27
+ F --> H[构建标准化响应]
28
+ G --> H
29
+ H --> I[设置安全响应头]
30
+ I --> J[返回错误响应]
31
+ ```
32
+
33
+ ## 错误分类系统
34
+
35
+ ### 错误分类枚举
36
+
37
+ ```typescript
38
+ enum ErrorCategory {
39
+ VALIDATION = 'VALIDATION_ERROR', // 验证错误
40
+ AUTHENTICATION = 'AUTH_ERROR', // 认证错误
41
+ AUTHORIZATION = 'AUTHZ_ERROR', // 授权错误
42
+ BUSINESS_LOGIC = 'BUSINESS_ERROR', // 业务逻辑错误
43
+ DATABASE = 'DATABASE_ERROR', // 数据库错误
44
+ EXTERNAL_SERVICE = 'EXTERNAL_SERVICE_ERROR', // 外部服务错误
45
+ SYSTEM = 'SYSTEM_ERROR', // 系统错误
46
+ NETWORK = 'NETWORK_ERROR', // 网络错误
47
+ UNKNOWN = 'UNKNOWN_ERROR' // 未知错误
48
+ }
49
+ ```
50
+
51
+ ### 错误码定义
52
+
53
+ ```typescript
54
+ enum ErrorCode {
55
+ // 4xx 客户端错误
56
+ INVALID_REQUEST = 'E400001', // 无效请求
57
+ VALIDATION_FAILED = 'E400002', // 验证失败
58
+ UNAUTHORIZED = 'E401001', // 未授权
59
+ FORBIDDEN = 'E403001', // 禁止访问
60
+ NOT_FOUND = 'E404001', // 资源不存在
61
+ METHOD_NOT_ALLOWED = 'E405001', // 方法不允许
62
+ CONFLICT = 'E409001', // 冲突
63
+
64
+ // 5xx 服务器错误
65
+ INTERNAL_ERROR = 'E500001', // 内部错误
66
+ DATABASE_ERROR = 'E500002', // 数据库错误
67
+ EXTERNAL_SERVICE_ERROR = 'E500003', // 外部服务错误
68
+ SERVICE_UNAVAILABLE = 'E503001', // 服务不可用
69
+ TIMEOUT = 'E504001' // 超时
70
+ }
71
+ ```
72
+
73
+ ## 安全防护
74
+
75
+ ### 敏感信息过滤
76
+
77
+ 系统自动过滤以下敏感字段:
78
+ - `password` - 密码
79
+ - `token` - 令牌
80
+ - `secret` - 密钥
81
+ - `key` - 密钥
82
+ - `auth` - 认证信息
83
+ - `credential` - 凭据
84
+
85
+ ### URL 安全过滤
86
+
87
+ - 支持相对路径和完整URL的安全过滤
88
+ - 自动移除查询参数中的敏感信息
89
+ - 容错处理,确保系统稳定性
90
+
91
+ ### 安全响应头
92
+
93
+ ```typescript
94
+ // 防止信息泄露
95
+ ctx.set('X-Content-Type-Options', 'nosniff');
96
+ ctx.set('X-Frame-Options', 'DENY');
97
+ ctx.set('X-XSS-Protection', '1; mode=block');
98
+
99
+ // 移除可能泄露信息的头
100
+ ctx.remove('X-Powered-By');
101
+ ctx.remove('Server');
102
+ ```
103
+
104
+ ## 日志记录
105
+
106
+ ### 结构化日志格式
107
+
108
+ ```json
109
+ {
110
+ "timestamp": "2025-10-25T23:01:31.441Z",
111
+ "level": "ERROR",
112
+ "category": "default",
113
+ "message": "企业级异常处理",
114
+ "service": "dp-koa-framework",
115
+ "version": "1.0.0",
116
+ "environment": "development",
117
+ "error": {
118
+ "category": "DATABASE_ERROR",
119
+ "code": "E500002",
120
+ "message": "数据库操作失败",
121
+ "stack": "...",
122
+ "statusCode": 500,
123
+ "severity": "high"
124
+ },
125
+ "request": {
126
+ "method": "POST",
127
+ "url": "/api/users?token=***REDACTED***",
128
+ "userAgent": "Mozilla/5.0...",
129
+ "ip": "127.0.0.1",
130
+ "requestId": "req-123456",
131
+ "userId": "user-789"
132
+ },
133
+ "context": {
134
+ "timestamp": "2025-10-25T23:01:31.441Z",
135
+ "environment": "development",
136
+ "service": "dp-koa-framework",
137
+ "version": "1.0.0"
138
+ }
139
+ }
140
+ ```
141
+
142
+ ## 监控告警
143
+
144
+ ### 告警触发条件
145
+
146
+ - **Critical** 级别错误:系统级错误,需要立即处理
147
+ - **High** 级别错误:严重错误,需要及时处理
148
+
149
+ ### 告警信息
150
+
151
+ ```json
152
+ {
153
+ "alert": {
154
+ "level": "high",
155
+ "category": "DATABASE_ERROR",
156
+ "code": "E500002",
157
+ "message": "数据库操作失败",
158
+ "timestamp": "2025-10-25T23:01:31.441Z"
159
+ },
160
+ "request": {
161
+ "method": "POST",
162
+ "url": "/api/users?token=***REDACTED***",
163
+ "ip": "127.0.0.1",
164
+ "requestId": "req-123456"
165
+ }
166
+ }
167
+ ```
168
+
169
+ ## 标准化响应
170
+
171
+ ### 响应格式
172
+
173
+ #### 生产环境响应
174
+ ```json
175
+ {
176
+ "success": false,
177
+ "error": {
178
+ "code": "E500002",
179
+ "category": "DATABASE_ERROR",
180
+ "message": "数据库操作失败",
181
+ "timestamp": "2025-10-25T23:01:31.441Z",
182
+ "requestId": "req-123456"
183
+ }
184
+ }
185
+ ```
186
+
187
+ #### 开发环境响应
188
+ ```json
189
+ {
190
+ "success": false,
191
+ "error": {
192
+ "code": "E500002",
193
+ "category": "DATABASE_ERROR",
194
+ "message": "数据库操作失败",
195
+ "timestamp": "2025-10-25T23:01:31.441Z",
196
+ "requestId": "req-123456"
197
+ },
198
+ "debug": {
199
+ "stack": "Error: ER_ACCESS_DENIED_ERROR...",
200
+ "originalError": { ... }
201
+ }
202
+ }
203
+ ```
204
+
205
+ ## 错误处理流程
206
+
207
+ ### 1. 异常捕获
208
+ ```typescript
209
+ try {
210
+ await next();
211
+ } catch (err) {
212
+ // 异常处理逻辑
213
+ }
214
+ ```
215
+
216
+ ### 2. 错误分类
217
+ - 根据错误类型自动分类
218
+ - 设置对应的错误码和状态码
219
+ - 确定错误严重程度
220
+
221
+ ### 3. 安全过滤
222
+ - 过滤敏感信息
223
+ - 安全化URL参数
224
+ - 保护系统信息
225
+
226
+ ### 4. 日志记录
227
+ - 记录结构化日志
228
+ - 包含完整上下文信息
229
+ - 支持日志分析和监控
230
+
231
+ ### 5. 告警触发
232
+ - 严重错误自动告警
233
+ - 可集成外部告警系统
234
+ - 支持多种告警方式
235
+
236
+ ### 6. 响应构建
237
+ - 构建标准化响应
238
+ - 环境差异化处理
239
+ - 设置安全响应头
240
+
241
+ ## 使用示例
242
+
243
+ ### 业务层抛出异常
244
+
245
+ ```typescript
246
+ // 验证错误
247
+ throw new Error('ValidationError: 用户名不能为空');
248
+
249
+ // 认证错误
250
+ const authError = new Error('UnauthorizedError');
251
+ authError.status = 401;
252
+ throw authError;
253
+
254
+ // 业务逻辑错误
255
+ const businessError = new Error('NotFoundError');
256
+ businessError.status = 404;
257
+ throw businessError;
258
+ ```
259
+
260
+ ### 框架层自动处理
261
+
262
+ 框架层会自动:
263
+ 1. 识别错误类型
264
+ 2. 分类和标准化
265
+ 3. 安全过滤
266
+ 4. 记录日志
267
+ 5. 触发告警(如需要)
268
+ 6. 返回标准化响应
269
+
270
+ ## 配置说明
271
+
272
+ ### 环境变量
273
+
274
+ - `NODE_ENV`: 环境标识(development/production)
275
+ - `npm_package_version`: 应用版本号
276
+
277
+ ### 敏感字段配置
278
+
279
+ 可在 `sanitizeErrorForLogging` 函数中自定义敏感字段列表:
280
+
281
+ ```typescript
282
+ const sensitiveFields = [
283
+ 'password', 'token', 'secret', 'key',
284
+ 'auth', 'credential', 'ssn', 'creditCard'
285
+ ];
286
+ ```
287
+
288
+ ## 最佳实践
289
+
290
+ ### 1. 错误抛出
291
+ - 使用标准的错误类型
292
+ - 设置合适的HTTP状态码
293
+ - 提供清晰的错误信息
294
+
295
+ ### 2. 日志监控
296
+ - 定期检查错误日志
297
+ - 关注严重错误告警
298
+ - 分析错误趋势和模式
299
+
300
+ ### 3. 安全防护
301
+ - 定期审查敏感字段列表
302
+ - 确保生产环境不泄露调试信息
303
+ - 监控异常访问模式
304
+
305
+ ### 4. 性能优化
306
+ - 避免在错误处理中执行耗时操作
307
+ - 合理设置告警阈值
308
+ - 优化日志记录性能
309
+
310
+ ## 扩展说明
311
+
312
+ ### 自定义错误分类
313
+
314
+ 可以通过修改 `classifyAndStandardizeError` 函数来添加自定义错误分类:
315
+
316
+ ```typescript
317
+ if (error.name === 'CustomBusinessError') {
318
+ return {
319
+ category: ErrorCategory.BUSINESS_LOGIC,
320
+ code: ErrorCode.CUSTOM_BUSINESS_ERROR,
321
+ statusCode: 400,
322
+ severity: 'medium',
323
+ message: '自定义业务错误'
324
+ };
325
+ }
326
+ ```
327
+
328
+ ### 集成外部告警系统
329
+
330
+ 可以在 `triggerErrorAlert` 函数中集成外部告警系统:
331
+
332
+ ```typescript
333
+ // 钉钉告警
334
+ await sendDingTalkAlert(errorInfo);
335
+
336
+ // 企业微信告警
337
+ await sendWeChatAlert(errorInfo);
338
+
339
+ // 邮件告警
340
+ await sendEmailAlert(errorInfo);
341
+ ```
342
+
343
+ ## 总结
344
+
345
+ 企业级异常处理系统提供了:
346
+
347
+ ✅ **完整的错误分类和标准化**
348
+ ✅ **强大的安全防护机制**
349
+ ✅ **结构化的日志记录**
350
+ ✅ **智能的监控告警**
351
+ ✅ **标准化的响应格式**
352
+ ✅ **环境适配能力**
353
+ ✅ **高度可扩展性**
354
+
355
+ 该系统完全符合企业开发规范,能够满足企业级应用的严格要求,为系统的稳定性和可维护性提供了强有力的保障。
356
+
357
+