@modern-js/main-doc 2.68.12 → 2.68.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.
@@ -47,6 +47,7 @@ export default defineServerConfig({
|
|
47
47
|
middlewares: [],
|
48
48
|
renderMiddlewares: [],
|
49
49
|
plugins: [],
|
50
|
+
onError: () => {},
|
50
51
|
});
|
51
52
|
```
|
52
53
|
|
@@ -71,6 +72,7 @@ type ServerConfig = {
|
|
71
72
|
middlewares?: MiddlewareObj[];
|
72
73
|
renderMiddlewares?: MiddlewareObj[];
|
73
74
|
plugins?: (ServerPlugin | ServerPluginLegacy)[];
|
75
|
+
onError?: (err: Error, c: Context) => Promise<any> | any;
|
74
76
|
}
|
75
77
|
```
|
76
78
|
|
@@ -231,6 +233,35 @@ export default () => {
|
|
231
233
|
```
|
232
234
|
|
233
235
|
|
236
|
+
### onError
|
237
|
+
|
238
|
+
`onError` is a global error handling function used to capture and handle all uncaught errors in the Modern.js server. By customizing the `onError` function, developers can uniformly handle different types of errors, return custom error responses, and implement features such as error logging and error classification.
|
239
|
+
|
240
|
+
Below is a basic example of an `onError` configuration:
|
241
|
+
|
242
|
+
```ts title="server/modern.server.ts"
|
243
|
+
import { defineServerConfig } from '@modern-js/server-runtime';
|
244
|
+
|
245
|
+
export default defineServerConfig({
|
246
|
+
onError: (err, c) => {
|
247
|
+
// Log the error
|
248
|
+
console.error('Server error:', err);
|
249
|
+
|
250
|
+
// Return different responses based on the error type
|
251
|
+
if (err instanceof SyntaxError) {
|
252
|
+
return c.json({ error: 'Invalid JSON' }, 400);
|
253
|
+
}
|
254
|
+
|
255
|
+
// Customize BFF error response based on request path
|
256
|
+
if (c.req.path.includes('/api')) {
|
257
|
+
return c.json({ message: 'API error occurred' }, 500);
|
258
|
+
}
|
259
|
+
|
260
|
+
return c.text('Internal Server Error', 500);
|
261
|
+
},
|
262
|
+
});
|
263
|
+
```
|
264
|
+
|
234
265
|
## Legacy API (Deprecated)
|
235
266
|
|
236
267
|
:::warning
|
@@ -267,9 +298,6 @@ const time: UnstableMiddleware = async (c: UnstableMiddlewareContext, next) => {
|
|
267
298
|
export const unstableMiddleware: UnstableMiddleware[] = [time];
|
268
299
|
```
|
269
300
|
|
270
|
-
:::info
|
271
|
-
For detailed API and more usage, see [UnstableMiddleware](/apis/app/runtime/web-server/unstable_middleware).
|
272
|
-
:::
|
273
301
|
|
274
302
|
### Hooks
|
275
303
|
|
@@ -302,9 +330,6 @@ Best practices when using Hooks:
|
|
302
330
|
2. Handle Rewrite and Redirect in afterMatch.
|
303
331
|
3. Inject HTML content in afterRender.
|
304
332
|
|
305
|
-
:::info
|
306
|
-
For detailed API and more usage, see [Hook](/apis/app/runtime/web-server/hook).
|
307
|
-
:::
|
308
333
|
|
309
334
|
|
310
335
|
## Migrate to the New Version of Custom Web Server
|
@@ -48,6 +48,7 @@ export default defineServerConfig({
|
|
48
48
|
middlewares: [], // 中间件
|
49
49
|
renderMiddlewares: [], // 渲染中间件
|
50
50
|
plugins: [], // 插件
|
51
|
+
onError: () => {}, // 错误处理
|
51
52
|
});
|
52
53
|
```
|
53
54
|
|
@@ -69,6 +70,7 @@ type ServerConfig = {
|
|
69
70
|
middlewares?: MiddlewareObj[];
|
70
71
|
renderMiddlewares?: MiddlewareObj[];
|
71
72
|
plugins?: (ServerPlugin | ServerPluginLegacy)[];
|
73
|
+
onError?: (err: Error, c: Context) => Promise<any> | any;
|
72
74
|
}
|
73
75
|
```
|
74
76
|
|
@@ -228,6 +230,36 @@ export default () => {
|
|
228
230
|
```
|
229
231
|
|
230
232
|
|
233
|
+
### onError
|
234
|
+
|
235
|
+
`onError` 是一个全局错误处理函数,用于捕获和处理 Modern.js server 中的所有未捕获错误。通过自定义 `onError` 函数,开发者可以统一处理不同类型的错误,返回自定义的错误响应,实现错误日志记录、错误分类处理等功能。
|
236
|
+
|
237
|
+
以下是一个基本的 `onError` 配置示例:
|
238
|
+
|
239
|
+
```ts title="server/modern.server.ts"
|
240
|
+
import { defineServerConfig } from '@modern-js/server-runtime';
|
241
|
+
|
242
|
+
export default defineServerConfig({
|
243
|
+
onError: (err, c) => {
|
244
|
+
// 记录错误日志
|
245
|
+
console.error('Server error:', err);
|
246
|
+
|
247
|
+
// 根据不同的错误类型返回不同的响应
|
248
|
+
if (err instanceof SyntaxError) {
|
249
|
+
return c.json({ error: 'Invalid JSON' }, 400);
|
250
|
+
}
|
251
|
+
|
252
|
+
// 根据请求路径定制 bff 异常响应
|
253
|
+
if (c.req.path.includes('/api')) {
|
254
|
+
return c.json({ message: 'API error occurred' }, 500);
|
255
|
+
}
|
256
|
+
|
257
|
+
return c.text('Internal Server Error', 500);
|
258
|
+
},
|
259
|
+
});
|
260
|
+
```
|
261
|
+
|
262
|
+
|
231
263
|
## 旧版 API(废弃)
|
232
264
|
|
233
265
|
:::warning
|
package/package.json
CHANGED
@@ -15,14 +15,14 @@
|
|
15
15
|
"modern",
|
16
16
|
"modern.js"
|
17
17
|
],
|
18
|
-
"version": "2.68.
|
18
|
+
"version": "2.68.13",
|
19
19
|
"publishConfig": {
|
20
20
|
"registry": "https://registry.npmjs.org/",
|
21
21
|
"access": "public"
|
22
22
|
},
|
23
23
|
"dependencies": {
|
24
24
|
"mermaid": "^11.4.1",
|
25
|
-
"@modern-js/sandpack-react": "2.68.
|
25
|
+
"@modern-js/sandpack-react": "2.68.13"
|
26
26
|
},
|
27
27
|
"devDependencies": {
|
28
28
|
"@rsbuild/plugin-sass": "1.4.0",
|