@linyjs/plugin-docs 0.0.13
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.
- package/README.md +137 -0
- package/docs/api-reference/index.md +361 -0
- package/docs/best-practices/error-handling.md +462 -0
- package/docs/best-practices/naming-conventions.md +496 -0
- package/docs/best-practices/performance.md +532 -0
- package/docs/best-practices/security.md +620 -0
- package/docs/best-practices/service-definition.md +332 -0
- package/docs/faq.md +322 -0
- package/docs/getting-started/first-plugin.md +358 -0
- package/docs/getting-started/installation.md +244 -0
- package/docs/getting-started/quick-start.md +235 -0
- package/docs/guides/authentication.md +428 -0
- package/docs/guides/client-module.md +604 -0
- package/docs/guides/data-persistence.md +640 -0
- package/docs/guides/permissions.md +440 -0
- package/docs/guides/server-module.md +377 -0
- package/docs/guides/ui-slots.md +475 -0
- package/docs/migration/v0-to-v1.md +354 -0
- package/examples/analytics-plugin/README.md +416 -0
- package/examples/analytics-plugin/dist/client/index.d.ts +36 -0
- package/examples/analytics-plugin/dist/client/index.js +173 -0
- package/examples/analytics-plugin/dist/index.d.ts +64 -0
- package/examples/analytics-plugin/dist/index.js +72 -0
- package/examples/analytics-plugin/dist/server/index.d.ts +119 -0
- package/examples/analytics-plugin/dist/server/index.js +313 -0
- package/examples/analytics-plugin/package.json +28 -0
- package/examples/analytics-plugin/src/client/index.tsx +414 -0
- package/examples/analytics-plugin/src/index.ts +71 -0
- package/examples/analytics-plugin/src/server/index.ts +349 -0
- package/examples/analytics-plugin/tsconfig.json +21 -0
- package/examples/blog-plugin/README.md +319 -0
- package/examples/blog-plugin/dist/client/index.d.ts +22 -0
- package/examples/blog-plugin/dist/client/index.js +197 -0
- package/examples/blog-plugin/dist/server/index.d.ts +5 -0
- package/examples/blog-plugin/dist/server/index.js +220 -0
- package/examples/blog-plugin/package.json +24 -0
- package/examples/blog-plugin/src/client/index.tsx +409 -0
- package/examples/blog-plugin/src/server/index.ts +240 -0
- package/examples/blog-plugin/tsconfig.json +16 -0
- package/examples/hello-world/README.md +101 -0
- package/examples/hello-world/dist/client/index.d.ts +5 -0
- package/examples/hello-world/dist/client/index.js +80 -0
- package/examples/hello-world/dist/server/index.d.ts +5 -0
- package/examples/hello-world/dist/server/index.js +38 -0
- package/examples/hello-world/node_modules/.package-lock.json +61 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/HOOKS_USAGE.md +281 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/IMPLEMENTATION_SUMMARY.md +166 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/QUICK_REFERENCE.md +130 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/README.md +214 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/package.json +26 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/application.ts +50 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/comparison-example.tsx +229 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/di.ts +127 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/domain.ts +52 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/hooks-example.tsx +275 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/hooks.ts +175 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/index.ts +87 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/interfaces.ts +117 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/src/router.ts +49 -0
- package/examples/hello-world/node_modules/@linyjs/client-module-interface/tsconfig.json +10 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/package.json +21 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/application.ts +21 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/di.ts +127 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/domain.ts +75 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/index.ts +55 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/src/interfaces.ts +58 -0
- package/examples/hello-world/node_modules/@linyjs/server-module-interface/tsconfig.json +8 -0
- package/examples/hello-world/node_modules/@types/react/LICENSE +21 -0
- package/examples/hello-world/node_modules/@types/react/README.md +15 -0
- package/examples/hello-world/node_modules/@types/react/canary.d.ts +129 -0
- package/examples/hello-world/node_modules/@types/react/compiler-runtime.d.ts +4 -0
- package/examples/hello-world/node_modules/@types/react/experimental.d.ts +184 -0
- package/examples/hello-world/node_modules/@types/react/global.d.ts +166 -0
- package/examples/hello-world/node_modules/@types/react/index.d.ts +4369 -0
- package/examples/hello-world/node_modules/@types/react/jsx-dev-runtime.d.ts +33 -0
- package/examples/hello-world/node_modules/@types/react/jsx-runtime.d.ts +24 -0
- package/examples/hello-world/node_modules/@types/react/package.json +210 -0
- package/examples/hello-world/node_modules/@types/react/ts5.0/canary.d.ts +129 -0
- package/examples/hello-world/node_modules/@types/react/ts5.0/experimental.d.ts +184 -0
- package/examples/hello-world/node_modules/@types/react/ts5.0/global.d.ts +166 -0
- package/examples/hello-world/node_modules/@types/react/ts5.0/index.d.ts +4356 -0
- package/examples/hello-world/node_modules/@types/react/ts5.0/jsx-dev-runtime.d.ts +33 -0
- package/examples/hello-world/node_modules/@types/react/ts5.0/jsx-runtime.d.ts +24 -0
- package/examples/hello-world/node_modules/csstype/LICENSE +19 -0
- package/examples/hello-world/node_modules/csstype/README.md +291 -0
- package/examples/hello-world/node_modules/csstype/index.d.ts +22569 -0
- package/examples/hello-world/node_modules/csstype/index.js.flow +6863 -0
- package/examples/hello-world/node_modules/csstype/package.json +70 -0
- package/examples/hello-world/node_modules/react/LICENSE +21 -0
- package/examples/hello-world/node_modules/react/README.md +37 -0
- package/examples/hello-world/node_modules/react/cjs/react-compiler-runtime.development.js +24 -0
- package/examples/hello-world/node_modules/react/cjs/react-compiler-runtime.production.js +16 -0
- package/examples/hello-world/node_modules/react/cjs/react-compiler-runtime.profiling.js +16 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.development.js +338 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.production.js +14 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.profiling.js +14 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.react-server.development.js +370 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-dev-runtime.react-server.production.js +40 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.development.js +352 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.production.js +34 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.profiling.js +34 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.react-server.development.js +370 -0
- package/examples/hello-world/node_modules/react/cjs/react-jsx-runtime.react-server.production.js +40 -0
- package/examples/hello-world/node_modules/react/cjs/react.development.js +1284 -0
- package/examples/hello-world/node_modules/react/cjs/react.production.js +542 -0
- package/examples/hello-world/node_modules/react/cjs/react.react-server.development.js +848 -0
- package/examples/hello-world/node_modules/react/cjs/react.react-server.production.js +423 -0
- package/examples/hello-world/node_modules/react/compiler-runtime.js +14 -0
- package/examples/hello-world/node_modules/react/index.js +7 -0
- package/examples/hello-world/node_modules/react/jsx-dev-runtime.js +7 -0
- package/examples/hello-world/node_modules/react/jsx-dev-runtime.react-server.js +7 -0
- package/examples/hello-world/node_modules/react/jsx-runtime.js +7 -0
- package/examples/hello-world/node_modules/react/jsx-runtime.react-server.js +7 -0
- package/examples/hello-world/node_modules/react/package.json +51 -0
- package/examples/hello-world/node_modules/react/react.react-server.js +7 -0
- package/examples/hello-world/node_modules/typescript/LICENSE.txt +55 -0
- package/examples/hello-world/node_modules/typescript/README.md +50 -0
- package/examples/hello-world/node_modules/typescript/SECURITY.md +41 -0
- package/examples/hello-world/node_modules/typescript/ThirdPartyNoticeText.txt +193 -0
- package/examples/hello-world/node_modules/typescript/bin/tsc +2 -0
- package/examples/hello-world/node_modules/typescript/bin/tsserver +2 -0
- package/examples/hello-world/node_modules/typescript/lib/_tsc.js +133818 -0
- package/examples/hello-world/node_modules/typescript/lib/_tsserver.js +659 -0
- package/examples/hello-world/node_modules/typescript/lib/_typingsInstaller.js +222 -0
- package/examples/hello-world/node_modules/typescript/lib/cs/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/de/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/es/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/fr/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/it/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/ja/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/ko/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.d.ts +22 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.decorators.d.ts +384 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.decorators.legacy.d.ts +22 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.dom.asynciterable.d.ts +41 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.dom.d.ts +39429 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.dom.iterable.d.ts +571 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.collection.d.ts +147 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.core.d.ts +597 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.d.ts +28 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.generator.d.ts +77 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.iterable.d.ts +605 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.promise.d.ts +81 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.proxy.d.ts +128 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.reflect.d.ts +144 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.symbol.d.ts +46 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts +326 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2016.array.include.d.ts +116 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2016.d.ts +21 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2016.full.d.ts +23 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2016.intl.d.ts +31 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.arraybuffer.d.ts +21 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.d.ts +26 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.date.d.ts +31 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.full.d.ts +23 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.intl.d.ts +44 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.object.d.ts +49 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts +135 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.string.d.ts +45 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2017.typedarrays.d.ts +53 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts +77 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.asynciterable.d.ts +53 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.intl.d.ts +83 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.promise.d.ts +30 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2018.regexp.d.ts +37 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.array.d.ts +79 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.intl.d.ts +23 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.object.d.ts +33 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.string.d.ts +37 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2019.symbol.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.bigint.d.ts +765 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.d.ts +27 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.date.d.ts +42 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.intl.d.ts +474 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.number.d.ts +28 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.promise.d.ts +47 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts +99 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.string.d.ts +44 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts +41 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2021.d.ts +23 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2021.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2021.intl.d.ts +166 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2021.promise.d.ts +48 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2021.string.d.ts +33 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2021.weakref.d.ts +78 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.array.d.ts +121 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.d.ts +25 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.error.d.ts +75 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.intl.d.ts +145 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.object.d.ts +26 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.regexp.d.ts +39 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2022.string.d.ts +25 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2023.array.d.ts +924 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2023.collection.d.ts +21 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2023.d.ts +22 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2023.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2023.intl.d.ts +56 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.arraybuffer.d.ts +65 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.collection.d.ts +29 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.d.ts +26 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.object.d.ts +29 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.promise.d.ts +35 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.regexp.d.ts +25 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.sharedmemory.d.ts +68 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es2024.string.d.ts +29 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es5.d.ts +4601 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.es6.d.ts +23 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.array.d.ts +35 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.collection.d.ts +96 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.d.ts +29 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.decorators.d.ts +28 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.disposable.d.ts +193 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.error.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.float16.d.ts +445 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.full.d.ts +24 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.intl.d.ts +21 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.iterator.d.ts +148 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.promise.d.ts +34 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.esnext.sharedmemory.d.ts +25 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.scripthost.d.ts +322 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.webworker.asynciterable.d.ts +41 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.webworker.d.ts +13150 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.webworker.importscripts.d.ts +23 -0
- package/examples/hello-world/node_modules/typescript/lib/lib.webworker.iterable.d.ts +340 -0
- package/examples/hello-world/node_modules/typescript/lib/pl/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/pt-br/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/ru/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/tr/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/tsc.js +8 -0
- package/examples/hello-world/node_modules/typescript/lib/tsserver.js +8 -0
- package/examples/hello-world/node_modules/typescript/lib/tsserverlibrary.d.ts +17 -0
- package/examples/hello-world/node_modules/typescript/lib/tsserverlibrary.js +21 -0
- package/examples/hello-world/node_modules/typescript/lib/typesMap.json +497 -0
- package/examples/hello-world/node_modules/typescript/lib/typescript.d.ts +11437 -0
- package/examples/hello-world/node_modules/typescript/lib/typescript.js +200276 -0
- package/examples/hello-world/node_modules/typescript/lib/typingsInstaller.js +8 -0
- package/examples/hello-world/node_modules/typescript/lib/watchGuard.js +53 -0
- package/examples/hello-world/node_modules/typescript/lib/zh-cn/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/lib/zh-tw/diagnosticMessages.generated.json +2122 -0
- package/examples/hello-world/node_modules/typescript/package.json +120 -0
- package/examples/hello-world/package-lock.json +74 -0
- package/examples/hello-world/package.json +23 -0
- package/examples/hello-world/src/client/index.tsx +110 -0
- package/examples/hello-world/src/server/index.ts +42 -0
- package/examples/hello-world/tsconfig.json +16 -0
- package/package.json +46 -0
- package/src/cli.ts +321 -0
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
# 错误处理
|
|
2
|
+
|
|
3
|
+
本指南提供 linyjs 插件开发中的错误处理最佳实践。
|
|
4
|
+
|
|
5
|
+
## 为什么需要错误处理?
|
|
6
|
+
|
|
7
|
+
- 🛡️ **提高应用稳定性** - 防止未捕获的错误导致整个应用崩溃
|
|
8
|
+
- 🎯 **改善用户体验** - 提供有意义的错误信息而不是技术细节
|
|
9
|
+
- 🔍 **便于调试** - 记录详细的错误日志帮助定位问题
|
|
10
|
+
- ✅ **可靠性保证** - 确保关键业务流程能够优雅降级
|
|
11
|
+
|
|
12
|
+
## 服务端错误处理
|
|
13
|
+
|
|
14
|
+
### 基础错误处理
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
handler: async (params, ctx) => {
|
|
18
|
+
try {
|
|
19
|
+
// 业务逻辑
|
|
20
|
+
const data = await fetchData();
|
|
21
|
+
return { success: true, data };
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.error('Error:', error);
|
|
24
|
+
return {
|
|
25
|
+
status: 500,
|
|
26
|
+
data: { error: 'Internal server error' }
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### 分类错误处理
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
handler: async (params, ctx) => {
|
|
36
|
+
try {
|
|
37
|
+
// 输入验证错误
|
|
38
|
+
if (!params.body.email) {
|
|
39
|
+
return {
|
|
40
|
+
status: 400,
|
|
41
|
+
data: { error: 'Email is required' }
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 业务逻辑错误
|
|
46
|
+
const user = await findUser(params.body.email);
|
|
47
|
+
if (!user) {
|
|
48
|
+
return {
|
|
49
|
+
status: 404,
|
|
50
|
+
data: { error: 'User not found' }
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 权限错误
|
|
55
|
+
if (!hasPermission(user, 'admin')) {
|
|
56
|
+
return {
|
|
57
|
+
status: 403,
|
|
58
|
+
data: { error: 'Insufficient permissions' }
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { success: true, data: user };
|
|
63
|
+
} catch (error) {
|
|
64
|
+
// 系统错误
|
|
65
|
+
console.error('Database error:', error);
|
|
66
|
+
return {
|
|
67
|
+
status: 500,
|
|
68
|
+
data: { error: 'Internal server error' }
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### 自定义错误类
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
// 定义自定义错误类
|
|
78
|
+
class ValidationError extends Error {
|
|
79
|
+
constructor(message: string, public field?: string) {
|
|
80
|
+
super(message);
|
|
81
|
+
this.name = 'ValidationError';
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
class AuthorizationError extends Error {
|
|
86
|
+
constructor(message: string = 'Unauthorized') {
|
|
87
|
+
super(message);
|
|
88
|
+
this.name = 'AuthorizationError';
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 在处理器中使用
|
|
93
|
+
handler: async (params, ctx) => {
|
|
94
|
+
try {
|
|
95
|
+
if (!isValidEmail(params.body.email)) {
|
|
96
|
+
throw new ValidationError('Invalid email format', 'email');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ...业务逻辑
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (error instanceof ValidationError) {
|
|
102
|
+
return {
|
|
103
|
+
status: 400,
|
|
104
|
+
data: { error: error.message, field: error.field }
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (error instanceof AuthorizationError) {
|
|
109
|
+
return {
|
|
110
|
+
status: 403,
|
|
111
|
+
data: { error: error.message }
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 处理未知错误
|
|
116
|
+
return {
|
|
117
|
+
status: 500,
|
|
118
|
+
data: { error: 'Internal server error' }
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### 全局错误处理中间件
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import type { IMiddlewareService } from '@linyjs/server-module-interface';
|
|
128
|
+
|
|
129
|
+
class ErrorHandlingMiddleware implements IMiddlewareService {
|
|
130
|
+
async process(payload: any, ctx: any, next: () => Promise<any>) {
|
|
131
|
+
try {
|
|
132
|
+
return await next();
|
|
133
|
+
} catch (error) {
|
|
134
|
+
// 记录错误日志
|
|
135
|
+
ctx.logger?.error('Unhandled error:', error);
|
|
136
|
+
|
|
137
|
+
// 统一错误响应格式
|
|
138
|
+
return {
|
|
139
|
+
status: 500,
|
|
140
|
+
data: {
|
|
141
|
+
error: 'Internal server error',
|
|
142
|
+
requestId: ctx.requestId
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export const errorMiddlewareDef = {
|
|
150
|
+
serviceTag: 'errorMiddleware',
|
|
151
|
+
serviceImpl: ErrorHandlingMiddleware,
|
|
152
|
+
meta: { usageType: 'middleware' }
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// 在所有路由中使用
|
|
156
|
+
{
|
|
157
|
+
middlewares: ['errorMiddleware'],
|
|
158
|
+
handler: async () => {
|
|
159
|
+
// 安全执行,错误会被中间件捕获
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## 客户端错误处理
|
|
165
|
+
|
|
166
|
+
### 基本错误边界
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
import React from 'react';
|
|
170
|
+
|
|
171
|
+
// 错误边界组件
|
|
172
|
+
class ErrorBoundary extends React.Component<
|
|
173
|
+
{ children: React.ReactNode },
|
|
174
|
+
{ hasError: boolean; error?: Error }
|
|
175
|
+
> {
|
|
176
|
+
constructor(props: { children: React.ReactNode }) {
|
|
177
|
+
super(props);
|
|
178
|
+
this.state = { hasError: false };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
static getDerivedStateFromError(error: Error) {
|
|
182
|
+
return { hasError: true, error };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
|
|
186
|
+
console.error('Component error:', error, errorInfo);
|
|
187
|
+
// 发送错误报告到服务器
|
|
188
|
+
this.reportError(error, errorInfo);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
reportError = (error: Error, errorInfo: React.ErrorInfo) => {
|
|
192
|
+
// 报告错误到监控系统
|
|
193
|
+
fetch('/api/error-report', {
|
|
194
|
+
method: 'POST',
|
|
195
|
+
body: JSON.stringify({
|
|
196
|
+
message: error.message,
|
|
197
|
+
stack: error.stack,
|
|
198
|
+
componentStack: errorInfo.componentStack
|
|
199
|
+
})
|
|
200
|
+
});
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
render() {
|
|
204
|
+
if (this.state.hasError) {
|
|
205
|
+
return (
|
|
206
|
+
<div className="error-fallback">
|
|
207
|
+
<h2>Something went wrong.</h2>
|
|
208
|
+
<p>We're sorry for the inconvenience.</p>
|
|
209
|
+
<button onClick={() => window.location.reload()}>
|
|
210
|
+
Refresh Page
|
|
211
|
+
</button>
|
|
212
|
+
</div>
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
return this.props.children;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// 使用错误边界
|
|
220
|
+
function App() {
|
|
221
|
+
return (
|
|
222
|
+
<ErrorBoundary>
|
|
223
|
+
<MyPluginComponent />
|
|
224
|
+
</ErrorBoundary>
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### 异步操作错误处理
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
function SafeComponent() {
|
|
233
|
+
const [data, setData] = useState(null);
|
|
234
|
+
const [error, setError] = useState<string | null>(null);
|
|
235
|
+
const [loading, setLoading] = useState(false);
|
|
236
|
+
|
|
237
|
+
const fetchData = async () => {
|
|
238
|
+
setLoading(true);
|
|
239
|
+
setError(null);
|
|
240
|
+
|
|
241
|
+
try {
|
|
242
|
+
const response = await fetch('/api/data');
|
|
243
|
+
|
|
244
|
+
if (!response.ok) {
|
|
245
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const result = await response.json();
|
|
249
|
+
setData(result);
|
|
250
|
+
} catch (err) {
|
|
251
|
+
setError(err instanceof Error ? err.message : 'Unknown error');
|
|
252
|
+
} finally {
|
|
253
|
+
setLoading(false);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
useEffect(() => {
|
|
258
|
+
fetchData();
|
|
259
|
+
}, []);
|
|
260
|
+
|
|
261
|
+
if (loading) {
|
|
262
|
+
return <div>Loading...</div>;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (error) {
|
|
266
|
+
return (
|
|
267
|
+
<div className="error-message">
|
|
268
|
+
<h3>Error loading data</h3>
|
|
269
|
+
<p>{error}</p>
|
|
270
|
+
<button onClick={fetchData}>Retry</button>
|
|
271
|
+
</div>
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return <div>{/* 正常显示数据 */}</div>;
|
|
276
|
+
}
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### 自定义 Hook 错误处理
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
import { useState, useCallback } from 'react';
|
|
283
|
+
|
|
284
|
+
interface FetchState<T> {
|
|
285
|
+
data: T | null;
|
|
286
|
+
error: string | null;
|
|
287
|
+
loading: boolean;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function useSafeFetch<T>() {
|
|
291
|
+
const [state, setState] = useState<FetchState<T>>({
|
|
292
|
+
data: null,
|
|
293
|
+
error: null,
|
|
294
|
+
loading: false
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
const fetchData = useCallback(async (url: string, options?: RequestInit) => {
|
|
298
|
+
setState({ data: null, error: null, loading: true });
|
|
299
|
+
|
|
300
|
+
try {
|
|
301
|
+
const response = await fetch(url, options);
|
|
302
|
+
|
|
303
|
+
if (!response.ok) {
|
|
304
|
+
throw new Error(`Request failed: ${response.statusText}`);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const data = await response.json();
|
|
308
|
+
setState({ data, error: null, loading: false });
|
|
309
|
+
return data;
|
|
310
|
+
} catch (error) {
|
|
311
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
312
|
+
setState({ data: null, error: message, loading: false });
|
|
313
|
+
throw error;
|
|
314
|
+
}
|
|
315
|
+
}, []);
|
|
316
|
+
|
|
317
|
+
return {
|
|
318
|
+
...state,
|
|
319
|
+
fetchData
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// 使用自定义 Hook
|
|
324
|
+
function DataComponent() {
|
|
325
|
+
const { data, error, loading, fetchData } = useSafeFetch<any>();
|
|
326
|
+
|
|
327
|
+
useEffect(() => {
|
|
328
|
+
fetchData('/api/users');
|
|
329
|
+
}, [fetchData]);
|
|
330
|
+
|
|
331
|
+
if (loading) return <div>Loading...</div>;
|
|
332
|
+
if (error) return <div>Error: {error}</div>;
|
|
333
|
+
|
|
334
|
+
return <div>{JSON.stringify(data)}</div>;
|
|
335
|
+
}
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### 全局错误处理
|
|
339
|
+
|
|
340
|
+
```typescript
|
|
341
|
+
// 设置全局错误处理器
|
|
342
|
+
window.addEventListener('error', (event) => {
|
|
343
|
+
console.error('Global error:', event.error);
|
|
344
|
+
|
|
345
|
+
// 发送错误报告
|
|
346
|
+
fetch('/api/error-report', {
|
|
347
|
+
method: 'POST',
|
|
348
|
+
headers: { 'Content-Type': 'application/json' },
|
|
349
|
+
body: JSON.stringify({
|
|
350
|
+
message: event.error?.message,
|
|
351
|
+
filename: event.filename,
|
|
352
|
+
lineno: event.lineno,
|
|
353
|
+
colno: event.colno,
|
|
354
|
+
stack: event.error?.stack
|
|
355
|
+
})
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
// 处理未处理的 Promise 拒绝
|
|
360
|
+
window.addEventListener('unhandledrejection', (event) => {
|
|
361
|
+
console.error('Unhandled promise rejection:', event.reason);
|
|
362
|
+
|
|
363
|
+
// 发送错误报告
|
|
364
|
+
fetch('/api/error-report', {
|
|
365
|
+
method: 'POST',
|
|
366
|
+
headers: { 'Content-Type': 'application/json' },
|
|
367
|
+
body: JSON.stringify({
|
|
368
|
+
type: 'unhandledrejection',
|
|
369
|
+
reason: event.reason?.toString(),
|
|
370
|
+
stack: event.reason?.stack
|
|
371
|
+
})
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
event.preventDefault();
|
|
375
|
+
});
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
## 错误处理最佳实践
|
|
379
|
+
|
|
380
|
+
### 1. 错误分类
|
|
381
|
+
|
|
382
|
+
```typescript
|
|
383
|
+
// 定义错误类型
|
|
384
|
+
enum ErrorType {
|
|
385
|
+
VALIDATION = 'VALIDATION',
|
|
386
|
+
NOT_FOUND = 'NOT_FOUND',
|
|
387
|
+
UNAUTHORIZED = 'UNAUTHORIZED',
|
|
388
|
+
FORBIDDEN = 'FORBIDDEN',
|
|
389
|
+
INTERNAL = 'INTERNAL',
|
|
390
|
+
NETWORK = 'NETWORK',
|
|
391
|
+
TIMEOUT = 'TIMEOUT'
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
interface AppError {
|
|
395
|
+
type: ErrorType;
|
|
396
|
+
message: string;
|
|
397
|
+
code?: string;
|
|
398
|
+
details?: any;
|
|
399
|
+
}
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### 2. 错误日志记录
|
|
403
|
+
|
|
404
|
+
```typescript
|
|
405
|
+
// 服务端日志记录
|
|
406
|
+
class LoggingService {
|
|
407
|
+
constructor(private logger: ILogger) {}
|
|
408
|
+
|
|
409
|
+
logError(error: Error, context?: any) {
|
|
410
|
+
this.logger.error('Application error', {
|
|
411
|
+
message: error.message,
|
|
412
|
+
stack: error.stack,
|
|
413
|
+
name: error.name,
|
|
414
|
+
context,
|
|
415
|
+
timestamp: new Date().toISOString()
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
logApiError(error: Error, request: any) {
|
|
420
|
+
this.logger.error('API error', {
|
|
421
|
+
message: error.message,
|
|
422
|
+
method: request.method,
|
|
423
|
+
url: request.url,
|
|
424
|
+
userAgent: request.headers['user-agent'],
|
|
425
|
+
ip: request.ip
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
### 3. 用户友好的错误消息
|
|
432
|
+
|
|
433
|
+
```typescript
|
|
434
|
+
// 错误消息映射
|
|
435
|
+
const ERROR_MESSAGES = {
|
|
436
|
+
'User not found': 'The requested user could not be found',
|
|
437
|
+
'Invalid email format': 'Please provide a valid email address',
|
|
438
|
+
'Insufficient permissions': 'You do not have permission to perform this action',
|
|
439
|
+
'Internal server error': 'Something went wrong. Please try again later'
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
function getUserFriendlyError(error: string): string {
|
|
443
|
+
return ERROR_MESSAGES[error as keyof typeof ERROR_MESSAGES] || error;
|
|
444
|
+
}
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
## 完整示例
|
|
448
|
+
|
|
449
|
+
查看错误处理实践:
|
|
450
|
+
|
|
451
|
+
- 📁 [Blog Plugin 示例](../../examples/blog-plugin/) - 完整的错误处理实现
|
|
452
|
+
- 📁 [Hello World 示例](../../examples/hello-world/) - 基础错误处理
|
|
453
|
+
|
|
454
|
+
## 相关文档
|
|
455
|
+
|
|
456
|
+
- 📝 [命名规范](./naming-conventions.md)
|
|
457
|
+
- ⚡ [性能优化](./performance.md)
|
|
458
|
+
- 🔒 [安全性](./security.md)
|
|
459
|
+
|
|
460
|
+
---
|
|
461
|
+
|
|
462
|
+
**上一篇**: [ServiceDef 代码风格规范](./service-definition.md) | **下一篇**: [性能优化](./performance.md)
|