@kevisual/router 0.0.80 → 0.0.82

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/dist/app.js CHANGED
@@ -2316,9 +2316,9 @@ var require_websocket = __commonJS((exports, module) => {
2316
2316
  emitErrorAndClose(websocket, err);
2317
2317
  });
2318
2318
  req.on("response", (res) => {
2319
- const location = res.headers.location;
2319
+ const location2 = res.headers.location;
2320
2320
  const statusCode = res.statusCode;
2321
- if (location && opts.followRedirects && statusCode >= 300 && statusCode < 400) {
2321
+ if (location2 && opts.followRedirects && statusCode >= 300 && statusCode < 400) {
2322
2322
  if (++websocket._redirects > opts.maxRedirects) {
2323
2323
  abortHandshake(websocket, req, "Maximum redirects exceeded");
2324
2324
  return;
@@ -2326,9 +2326,9 @@ var require_websocket = __commonJS((exports, module) => {
2326
2326
  req.abort();
2327
2327
  let addr;
2328
2328
  try {
2329
- addr = new URL2(location, address);
2329
+ addr = new URL2(location2, address);
2330
2330
  } catch (e) {
2331
- const err = new SyntaxError(`Invalid URL: ${location}`);
2331
+ const err = new SyntaxError(`Invalid URL: ${location2}`);
2332
2332
  emitErrorAndClose(websocket, err);
2333
2333
  return;
2334
2334
  }
@@ -3019,18 +3019,18 @@ class CustomError extends Error {
3019
3019
  code;
3020
3020
  data;
3021
3021
  message;
3022
- tips;
3023
- constructor(code, message, tips) {
3024
- super(message || String(code));
3025
- this.name = "CustomError";
3026
- if (typeof code === "number") {
3027
- this.code = code;
3028
- this.message = message;
3029
- } else {
3030
- this.code = 500;
3031
- this.message = code;
3032
- }
3033
- this.tips = tips;
3022
+ constructor(code, opts) {
3023
+ if (typeof code === "object" && code !== null) {
3024
+ opts = code;
3025
+ code = opts.code || 500;
3026
+ }
3027
+ let message = opts?.message || String(code);
3028
+ const cause = opts?.cause;
3029
+ super(message, { cause });
3030
+ this.name = "RouterError";
3031
+ let codeNum = opts?.code || (typeof code === "number" ? code : undefined);
3032
+ this.code = codeNum ?? 500;
3033
+ this.message = message;
3034
3034
  Error.captureStackTrace(this, this.constructor);
3035
3035
  }
3036
3036
  static fromCode(code) {
@@ -3045,8 +3045,7 @@ class CustomError extends Error {
3045
3045
  return {
3046
3046
  code: e?.code,
3047
3047
  data: e?.data,
3048
- message: e?.message,
3049
- tips: e?.tips
3048
+ message: e?.message
3050
3049
  };
3051
3050
  }
3052
3051
  static isError(error) {
@@ -3060,8 +3059,7 @@ class CustomError extends Error {
3060
3059
  return {
3061
3060
  code: e2?.code,
3062
3061
  data: e2?.data,
3063
- message: e2?.message,
3064
- tips: e2?.tips
3062
+ message: e2?.message
3065
3063
  };
3066
3064
  }
3067
3065
  }
@@ -17300,7 +17298,16 @@ class QueryRouter {
17300
17298
  this.importRoutes(router.routes);
17301
17299
  }
17302
17300
  throw(...args) {
17303
- throw new CustomError(...args);
17301
+ const [args0, args1] = args;
17302
+ if (args0 && typeof args0 === "object") {
17303
+ throw new CustomError(args0);
17304
+ }
17305
+ if (args1 && typeof args1 === "object") {
17306
+ throw new CustomError(args0, args1);
17307
+ } else if (args1) {
17308
+ throw new CustomError(args0, { message: args1 });
17309
+ }
17310
+ throw new CustomError(args0);
17304
17311
  }
17305
17312
  hasRoute(path, key = "") {
17306
17313
  return this.routes.find((r) => r.path === path && r.key === key);
@@ -17744,8 +17751,13 @@ class ServerBase {
17744
17751
  res.end(JSON.stringify(end));
17745
17752
  }
17746
17753
  } catch (e) {
17747
- console.error(e);
17748
17754
  res.setHeader("Content-Type", "application/json; charset=utf-8");
17755
+ if (CustomError.isError(e)) {
17756
+ const parsedError = CustomError.parseError(e);
17757
+ res.end(JSON.stringify(parsedError));
17758
+ return;
17759
+ }
17760
+ console.error(e);
17749
17761
  if (e.code && typeof e.code === "number") {
17750
17762
  res.end(resultError(e.message || `Router Server error`, e.code));
17751
17763
  } else {
@@ -18321,7 +18333,8 @@ class App extends QueryRouter {
18321
18333
  }
18322
18334
  }
18323
18335
 
18324
- // node_modules/.pnpm/@kevisual+context@0.0.6/node_modules/@kevisual/context/dist/app.js
18336
+ // node_modules/.pnpm/@kevisual+context@0.0.8/node_modules/@kevisual/context/dist/app.js
18337
+ var isBrowser2 = typeof window !== "undefined" && typeof window.document !== "undefined";
18325
18338
  function getDefaultExportFromCjs(x) {
18326
18339
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
18327
18340
  }
@@ -18785,7 +18798,7 @@ var useEnv = (initEnv, initKey = "config", isOverwrite) => {
18785
18798
  }
18786
18799
  return gt[initKey];
18787
18800
  };
18788
- var useEnvKey = (key, init, initKey = "config") => {
18801
+ var useEnvKey = (key, init, initKey = "config", opts = {}) => {
18789
18802
  const _env = useEnv({}, initKey);
18790
18803
  if (key && typeof _env[key] !== "undefined") {
18791
18804
  return _env[key];
@@ -18811,12 +18824,13 @@ var useEnvKey = (key, init, initKey = "config") => {
18811
18824
  const voidFn = async () => {
18812
18825
  return _env[key];
18813
18826
  };
18827
+ const timeout = opts.timeout || 5 * 60 * 1000;
18814
18828
  const checkFn = async () => {
18815
18829
  const loadRes = await baseLoad.load(voidFn, {
18816
18830
  key,
18817
18831
  isReRun: true,
18818
18832
  checkSuccess: () => _env[key],
18819
- timeout: 5 * 60 * 1000,
18833
+ timeout,
18820
18834
  interval: 1000
18821
18835
  });
18822
18836
  if (loadRes.code !== 200) {
@@ -18832,27 +18846,28 @@ var useEnvKey = (key, init, initKey = "config") => {
18832
18846
  };
18833
18847
  var useEnvKeyNew = (key, initKey = "config", opts) => {
18834
18848
  const _env = useEnv({}, initKey);
18849
+ const timeout = opts?.timeout;
18835
18850
  if (key) {
18836
18851
  delete _env[key];
18837
18852
  }
18838
18853
  if (opts?.getNew && opts.init) {
18839
- return useEnvKey(key, opts.init, initKey);
18854
+ return useEnvKey(key, opts.init, initKey, { timeout });
18840
18855
  } else if (opts?.getNew) {
18841
- return useEnvKey(key, null, initKey);
18856
+ return useEnvKey(key, null, initKey, { timeout });
18842
18857
  }
18843
18858
  };
18844
- var useContextKey = (key, init, isNew) => {
18845
- if (isNew) {
18846
- return useEnvKeyNew(key, "context", { getNew: true, init });
18859
+ var useContextKey = (key, init, opts) => {
18860
+ if (opts?.isNew) {
18861
+ return useEnvKeyNew(key, "context", { getNew: true, init, ...opts });
18847
18862
  }
18848
- return useEnvKey(key, init, "context");
18863
+ return useEnvKey(key, init, "context", opts);
18849
18864
  };
18850
18865
  var use = useContextKey;
18851
- var useConfigKey = (key, init, isNew) => {
18852
- if (isNew) {
18853
- return useEnvKeyNew(key, "config", { getNew: true, init });
18866
+ var useConfigKey = (key, init, opts) => {
18867
+ if (opts?.isNew) {
18868
+ return useEnvKeyNew(key, "config", { getNew: true, init, ...opts });
18854
18869
  }
18855
- return useEnvKey(key, init, "config");
18870
+ return useEnvKey(key, init, "config", opts);
18856
18871
  };
18857
18872
 
18858
18873
  class InitEnv {
@@ -19522,7 +19537,7 @@ app
19522
19537
  10. **中间件找不到会返回 404**,错误信息中会包含找不到的中间件列表。
19523
19538
  `;
19524
19539
  // package.json
19525
- var version2 = "0.0.80";
19540
+ var version2 = "0.0.82";
19526
19541
 
19527
19542
  // agent/routes/route-create.ts
19528
19543
  app.route({
@@ -5,6 +5,12 @@ import { IncomingMessage, ServerResponse } from 'node:http';
5
5
  import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
6
6
  import { PluginInput, Hooks, Plugin } from '@opencode-ai/plugin';
7
7
 
8
+ type CustomErrorOptions = {
9
+ cause?: Error | string;
10
+ code?: number;
11
+ message?: string;
12
+ };
13
+
8
14
  declare class MockProcess {
9
15
  emitter?: EventEmitter;
10
16
  process?: NodeJS.Process;
@@ -314,7 +320,9 @@ declare class QueryRouter {
314
320
  }, SimpleObject>[];
315
321
  importRoutes(routes: Route[]): void;
316
322
  importRouter(router: QueryRouter): void;
317
- throw(code?: number | string, message?: string, tips?: string): void;
323
+ throw(code?: number | string, message?: string): void;
324
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
325
+ throw(opts?: CustomErrorOptions): void;
318
326
  hasRoute(path: string, key?: string): Route<{
319
327
  [key: string]: any;
320
328
  }, SimpleObject>;
package/dist/opencode.js CHANGED
@@ -203,7 +203,8 @@ var require_eventemitter3 = __commonJS((exports, module) => {
203
203
  }
204
204
  });
205
205
 
206
- // node_modules/.pnpm/@kevisual+context@0.0.6/node_modules/@kevisual/context/dist/app.js
206
+ // node_modules/.pnpm/@kevisual+context@0.0.8/node_modules/@kevisual/context/dist/app.js
207
+ var isBrowser2 = typeof window !== "undefined" && typeof window.document !== "undefined";
207
208
  function getDefaultExportFromCjs(x) {
208
209
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
209
210
  }
@@ -667,7 +668,7 @@ var useEnv = (initEnv, initKey = "config", isOverwrite) => {
667
668
  }
668
669
  return gt[initKey];
669
670
  };
670
- var useEnvKey = (key, init, initKey = "config") => {
671
+ var useEnvKey = (key, init, initKey = "config", opts = {}) => {
671
672
  const _env = useEnv({}, initKey);
672
673
  if (key && typeof _env[key] !== "undefined") {
673
674
  return _env[key];
@@ -693,12 +694,13 @@ var useEnvKey = (key, init, initKey = "config") => {
693
694
  const voidFn = async () => {
694
695
  return _env[key];
695
696
  };
697
+ const timeout = opts.timeout || 5 * 60 * 1000;
696
698
  const checkFn = async () => {
697
699
  const loadRes = await baseLoad.load(voidFn, {
698
700
  key,
699
701
  isReRun: true,
700
702
  checkSuccess: () => _env[key],
701
- timeout: 5 * 60 * 1000,
703
+ timeout,
702
704
  interval: 1000
703
705
  });
704
706
  if (loadRes.code !== 200) {
@@ -714,27 +716,28 @@ var useEnvKey = (key, init, initKey = "config") => {
714
716
  };
715
717
  var useEnvKeyNew = (key, initKey = "config", opts) => {
716
718
  const _env = useEnv({}, initKey);
719
+ const timeout = opts?.timeout;
717
720
  if (key) {
718
721
  delete _env[key];
719
722
  }
720
723
  if (opts?.getNew && opts.init) {
721
- return useEnvKey(key, opts.init, initKey);
724
+ return useEnvKey(key, opts.init, initKey, { timeout });
722
725
  } else if (opts?.getNew) {
723
- return useEnvKey(key, null, initKey);
726
+ return useEnvKey(key, null, initKey, { timeout });
724
727
  }
725
728
  };
726
- var useContextKey = (key, init, isNew) => {
727
- if (isNew) {
728
- return useEnvKeyNew(key, "context", { getNew: true, init });
729
+ var useContextKey = (key, init, opts) => {
730
+ if (opts?.isNew) {
731
+ return useEnvKeyNew(key, "context", { getNew: true, init, ...opts });
729
732
  }
730
- return useEnvKey(key, init, "context");
733
+ return useEnvKey(key, init, "context", opts);
731
734
  };
732
735
  var use = useContextKey;
733
- var useConfigKey = (key, init, isNew) => {
734
- if (isNew) {
735
- return useEnvKeyNew(key, "config", { getNew: true, init });
736
+ var useConfigKey = (key, init, opts) => {
737
+ if (opts?.isNew) {
738
+ return useEnvKeyNew(key, "config", { getNew: true, init, ...opts });
736
739
  }
737
- return useEnvKey(key, init, "config");
740
+ return useEnvKey(key, init, "config", opts);
738
741
  };
739
742
 
740
743
  class InitEnv {
@@ -2,6 +2,37 @@ import { EventEmitter } from 'eventemitter3';
2
2
  import { z } from 'zod';
3
3
  import { Query, DataOpts, Result } from '@kevisual/query/query';
4
4
 
5
+ type CustomErrorOptions = {
6
+ cause?: Error | string;
7
+ code?: number;
8
+ message?: string;
9
+ };
10
+ /** 自定义错误 */
11
+ declare class CustomError extends Error {
12
+ code?: number;
13
+ data?: any;
14
+ message: string;
15
+ constructor(code?: number | string | CustomErrorOptions, opts?: CustomErrorOptions);
16
+ static fromCode(code?: number): CustomError;
17
+ static fromErrorData(code?: number, data?: any): CustomError;
18
+ static parseError(e: CustomError): {
19
+ code: number;
20
+ data: any;
21
+ message: string;
22
+ };
23
+ /**
24
+ * 判断 throw 的错误是否不是当前这个错误
25
+ * @param err
26
+ * @returns
27
+ */
28
+ static isError(error: unknown): error is CustomError;
29
+ parse(e?: CustomError): {
30
+ code: number;
31
+ data: any;
32
+ message: string;
33
+ };
34
+ }
35
+
5
36
  declare class MockProcess {
6
37
  emitter?: EventEmitter;
7
38
  process?: NodeJS.Process;
@@ -358,7 +389,9 @@ declare class QueryRouter {
358
389
  }, SimpleObject$1>[];
359
390
  importRoutes(routes: Route[]): void;
360
391
  importRouter(router: QueryRouter): void;
361
- throw(code?: number | string, message?: string, tips?: string): void;
392
+ throw(code?: number | string, message?: string): void;
393
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
394
+ throw(opts?: CustomErrorOptions): void;
362
395
  hasRoute(path: string, key?: string): Route<{
363
396
  [key: string]: any;
364
397
  }, SimpleObject$1>;
@@ -498,35 +531,6 @@ declare const createSchema: (rule: Rule) => z.ZodType<any, any, any>;
498
531
 
499
532
  type Schema = z.ZodType<any, any, any>;
500
533
 
501
- /** 自定义错误 */
502
- declare class CustomError extends Error {
503
- code?: number;
504
- data?: any;
505
- message: string;
506
- tips?: string;
507
- constructor(code?: number | string, message?: string, tips?: string);
508
- static fromCode(code?: number): CustomError;
509
- static fromErrorData(code?: number, data?: any): CustomError;
510
- static parseError(e: CustomError): {
511
- code: number;
512
- data: any;
513
- message: string;
514
- tips: string;
515
- };
516
- /**
517
- * 判断 throw 的错误是否不是当前这个错误
518
- * @param err
519
- * @returns
520
- */
521
- static isError(error: unknown): error is CustomError;
522
- parse(e?: CustomError): {
523
- code: number;
524
- data: any;
525
- message: string;
526
- tips: string;
527
- };
528
- }
529
-
530
534
  type RouteObject = {
531
535
  [key: string]: RouteOpts;
532
536
  };
@@ -208,18 +208,18 @@ class CustomError extends Error {
208
208
  code;
209
209
  data;
210
210
  message;
211
- tips;
212
- constructor(code, message, tips) {
213
- super(message || String(code));
214
- this.name = "CustomError";
215
- if (typeof code === "number") {
216
- this.code = code;
217
- this.message = message;
218
- } else {
219
- this.code = 500;
220
- this.message = code;
221
- }
222
- this.tips = tips;
211
+ constructor(code, opts) {
212
+ if (typeof code === "object" && code !== null) {
213
+ opts = code;
214
+ code = opts.code || 500;
215
+ }
216
+ let message = opts?.message || String(code);
217
+ const cause = opts?.cause;
218
+ super(message, { cause });
219
+ this.name = "RouterError";
220
+ let codeNum = opts?.code || (typeof code === "number" ? code : undefined);
221
+ this.code = codeNum ?? 500;
222
+ this.message = message;
223
223
  Error.captureStackTrace(this, this.constructor);
224
224
  }
225
225
  static fromCode(code) {
@@ -234,8 +234,7 @@ class CustomError extends Error {
234
234
  return {
235
235
  code: e?.code,
236
236
  data: e?.data,
237
- message: e?.message,
238
- tips: e?.tips
237
+ message: e?.message
239
238
  };
240
239
  }
241
240
  static isError(error) {
@@ -249,8 +248,7 @@ class CustomError extends Error {
249
248
  return {
250
249
  code: e2?.code,
251
250
  data: e2?.data,
252
- message: e2?.message,
253
- tips: e2?.tips
251
+ message: e2?.message
254
252
  };
255
253
  }
256
254
  }
@@ -14464,7 +14462,16 @@ class QueryRouter {
14464
14462
  this.importRoutes(router.routes);
14465
14463
  }
14466
14464
  throw(...args) {
14467
- throw new CustomError(...args);
14465
+ const [args0, args1] = args;
14466
+ if (args0 && typeof args0 === "object") {
14467
+ throw new CustomError(args0);
14468
+ }
14469
+ if (args1 && typeof args1 === "object") {
14470
+ throw new CustomError(args0, args1);
14471
+ } else if (args1) {
14472
+ throw new CustomError(args0, { message: args1 });
14473
+ }
14474
+ throw new CustomError(args0);
14468
14475
  }
14469
14476
  hasRoute(path, key = "") {
14470
14477
  return this.routes.find((r) => r.path === path && r.key === key);
@@ -2,6 +2,12 @@ import { EventEmitter } from 'eventemitter3';
2
2
  import { z } from 'zod';
3
3
  import { Query, DataOpts, Result } from '@kevisual/query/query';
4
4
 
5
+ type CustomErrorOptions = {
6
+ cause?: Error | string;
7
+ code?: number;
8
+ message?: string;
9
+ };
10
+
5
11
  declare class MockProcess {
6
12
  emitter?: EventEmitter;
7
13
  process?: NodeJS.Process;
@@ -311,7 +317,9 @@ declare class QueryRouter {
311
317
  }, SimpleObject$1>[];
312
318
  importRoutes(routes: Route[]): void;
313
319
  importRouter(router: QueryRouter): void;
314
- throw(code?: number | string, message?: string, tips?: string): void;
320
+ throw(code?: number | string, message?: string): void;
321
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
322
+ throw(opts?: CustomErrorOptions): void;
315
323
  hasRoute(path: string, key?: string): Route<{
316
324
  [key: string]: any;
317
325
  }, SimpleObject$1>;
package/dist/router.d.ts CHANGED
@@ -8,6 +8,37 @@ import http2 from 'node:http2';
8
8
  import { WebSocketServer } from 'ws';
9
9
  import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
10
10
 
11
+ type CustomErrorOptions = {
12
+ cause?: Error | string;
13
+ code?: number;
14
+ message?: string;
15
+ };
16
+ /** 自定义错误 */
17
+ declare class CustomError extends Error {
18
+ code?: number;
19
+ data?: any;
20
+ message: string;
21
+ constructor(code?: number | string | CustomErrorOptions, opts?: CustomErrorOptions);
22
+ static fromCode(code?: number): CustomError;
23
+ static fromErrorData(code?: number, data?: any): CustomError;
24
+ static parseError(e: CustomError): {
25
+ code: number;
26
+ data: any;
27
+ message: string;
28
+ };
29
+ /**
30
+ * 判断 throw 的错误是否不是当前这个错误
31
+ * @param err
32
+ * @returns
33
+ */
34
+ static isError(error: unknown): error is CustomError;
35
+ parse(e?: CustomError): {
36
+ code: number;
37
+ data: any;
38
+ message: string;
39
+ };
40
+ }
41
+
11
42
  declare class MockProcess {
12
43
  emitter?: EventEmitter;
13
44
  process?: NodeJS.Process;
@@ -364,7 +395,9 @@ declare class QueryRouter {
364
395
  }, SimpleObject$1>[];
365
396
  importRoutes(routes: Route[]): void;
366
397
  importRouter(router: QueryRouter): void;
367
- throw(code?: number | string, message?: string, tips?: string): void;
398
+ throw(code?: number | string, message?: string): void;
399
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
400
+ throw(opts?: CustomErrorOptions): void;
368
401
  hasRoute(path: string, key?: string): Route<{
369
402
  [key: string]: any;
370
403
  }, SimpleObject$1>;
@@ -504,35 +537,6 @@ declare const createSchema: (rule: Rule) => z.ZodType<any, any, any>;
504
537
 
505
538
  type Schema = z.ZodType<any, any, any>;
506
539
 
507
- /** 自定义错误 */
508
- declare class CustomError extends Error {
509
- code?: number;
510
- data?: any;
511
- message: string;
512
- tips?: string;
513
- constructor(code?: number | string, message?: string, tips?: string);
514
- static fromCode(code?: number): CustomError;
515
- static fromErrorData(code?: number, data?: any): CustomError;
516
- static parseError(e: CustomError): {
517
- code: number;
518
- data: any;
519
- message: string;
520
- tips: string;
521
- };
522
- /**
523
- * 判断 throw 的错误是否不是当前这个错误
524
- * @param err
525
- * @returns
526
- */
527
- static isError(error: unknown): error is CustomError;
528
- parse(e?: CustomError): {
529
- code: number;
530
- data: any;
531
- message: string;
532
- tips: string;
533
- };
534
- }
535
-
536
540
  type RouteObject = {
537
541
  [key: string]: RouteOpts;
538
542
  };
package/dist/router.js CHANGED
@@ -3019,18 +3019,18 @@ class CustomError extends Error {
3019
3019
  code;
3020
3020
  data;
3021
3021
  message;
3022
- tips;
3023
- constructor(code, message, tips) {
3024
- super(message || String(code));
3025
- this.name = "CustomError";
3026
- if (typeof code === "number") {
3027
- this.code = code;
3028
- this.message = message;
3029
- } else {
3030
- this.code = 500;
3031
- this.message = code;
3032
- }
3033
- this.tips = tips;
3022
+ constructor(code, opts) {
3023
+ if (typeof code === "object" && code !== null) {
3024
+ opts = code;
3025
+ code = opts.code || 500;
3026
+ }
3027
+ let message = opts?.message || String(code);
3028
+ const cause = opts?.cause;
3029
+ super(message, { cause });
3030
+ this.name = "RouterError";
3031
+ let codeNum = opts?.code || (typeof code === "number" ? code : undefined);
3032
+ this.code = codeNum ?? 500;
3033
+ this.message = message;
3034
3034
  Error.captureStackTrace(this, this.constructor);
3035
3035
  }
3036
3036
  static fromCode(code) {
@@ -3045,8 +3045,7 @@ class CustomError extends Error {
3045
3045
  return {
3046
3046
  code: e?.code,
3047
3047
  data: e?.data,
3048
- message: e?.message,
3049
- tips: e?.tips
3048
+ message: e?.message
3050
3049
  };
3051
3050
  }
3052
3051
  static isError(error) {
@@ -3060,8 +3059,7 @@ class CustomError extends Error {
3060
3059
  return {
3061
3060
  code: e2?.code,
3062
3061
  data: e2?.data,
3063
- message: e2?.message,
3064
- tips: e2?.tips
3062
+ message: e2?.message
3065
3063
  };
3066
3064
  }
3067
3065
  }
@@ -17297,7 +17295,16 @@ class QueryRouter {
17297
17295
  this.importRoutes(router.routes);
17298
17296
  }
17299
17297
  throw(...args) {
17300
- throw new CustomError(...args);
17298
+ const [args0, args1] = args;
17299
+ if (args0 && typeof args0 === "object") {
17300
+ throw new CustomError(args0);
17301
+ }
17302
+ if (args1 && typeof args1 === "object") {
17303
+ throw new CustomError(args0, args1);
17304
+ } else if (args1) {
17305
+ throw new CustomError(args0, { message: args1 });
17306
+ }
17307
+ throw new CustomError(args0);
17301
17308
  }
17302
17309
  hasRoute(path, key = "") {
17303
17310
  return this.routes.find((r) => r.path === path && r.key === key);
@@ -17972,8 +17979,13 @@ class ServerBase {
17972
17979
  res.end(JSON.stringify(end));
17973
17980
  }
17974
17981
  } catch (e) {
17975
- console.error(e);
17976
17982
  res.setHeader("Content-Type", "application/json; charset=utf-8");
17983
+ if (CustomError.isError(e)) {
17984
+ const parsedError = CustomError.parseError(e);
17985
+ res.end(JSON.stringify(parsedError));
17986
+ return;
17987
+ }
17988
+ console.error(e);
17977
17989
  if (e.code && typeof e.code === "number") {
17978
17990
  res.end(resultError(e.message || `Router Server error`, e.code));
17979
17991
  } else {
package/dist/ws.d.ts CHANGED
@@ -52,6 +52,12 @@ declare class ReconnectingWebSocket {
52
52
  getRetryCount(): number;
53
53
  }
54
54
 
55
+ type CustomErrorOptions = {
56
+ cause?: Error | string;
57
+ code?: number;
58
+ message?: string;
59
+ };
60
+
55
61
  declare class MockProcess {
56
62
  emitter?: EventEmitter;
57
63
  process?: NodeJS.Process;
@@ -361,7 +367,9 @@ declare class QueryRouter {
361
367
  }, SimpleObject>[];
362
368
  importRoutes(routes: Route[]): void;
363
369
  importRouter(router: QueryRouter): void;
364
- throw(code?: number | string, message?: string, tips?: string): void;
370
+ throw(code?: number | string, message?: string): void;
371
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
372
+ throw(opts?: CustomErrorOptions): void;
365
373
  hasRoute(path: string, key?: string): Route<{
366
374
  [key: string]: any;
367
375
  }, SimpleObject>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@kevisual/router",
4
- "version": "0.0.80",
4
+ "version": "0.0.82",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
@@ -23,21 +23,21 @@
23
23
  "license": "MIT",
24
24
  "devDependencies": {
25
25
  "@kevisual/code-builder": "^0.0.6",
26
- "@kevisual/context": "^0.0.6",
26
+ "@kevisual/context": "^0.0.8",
27
27
  "@kevisual/dts": "^0.0.4",
28
28
  "@kevisual/js-filter": "^0.0.5",
29
29
  "@kevisual/local-proxy": "^0.0.8",
30
- "@kevisual/query": "^0.0.47",
30
+ "@kevisual/query": "^0.0.49",
31
31
  "@kevisual/use-config": "^1.0.30",
32
- "@opencode-ai/plugin": "^1.2.6",
32
+ "@opencode-ai/plugin": "^1.2.10",
33
33
  "@types/bun": "^1.3.9",
34
- "@types/node": "^25.2.3",
34
+ "@types/node": "^25.3.0",
35
35
  "@types/send": "^1.2.1",
36
36
  "@types/ws": "^8.18.1",
37
37
  "@types/xml2js": "^0.4.14",
38
38
  "eventemitter3": "^5.0.4",
39
39
  "fast-glob": "^3.3.3",
40
- "hono": "^4.11.9",
40
+ "hono": "^4.12.0",
41
41
  "nanoid": "^5.1.6",
42
42
  "path-to-regexp": "^8.3.0",
43
43
  "send": "^1.2.1",
@@ -1,20 +1,25 @@
1
+ export type CustomErrorOptions = {
2
+ cause?: Error | string;
3
+ code?: number;
4
+ message?: string;
5
+ }
1
6
  /** 自定义错误 */
2
7
  export class CustomError extends Error {
3
8
  code?: number;
4
9
  data?: any;
5
10
  message: string;
6
- tips?: string;
7
- constructor(code?: number | string, message?: string, tips?: string) {
8
- super(message || String(code));
9
- this.name = 'CustomError';
10
- if (typeof code === 'number') {
11
- this.code = code;
12
- this.message = message!;
13
- } else {
14
- this.code = 500;
15
- this.message = code!;
11
+ constructor(code?: number | string | CustomErrorOptions, opts?: CustomErrorOptions) {
12
+ if (typeof code === 'object' && code !== null) {
13
+ opts = code;
14
+ code = opts.code || 500;
16
15
  }
17
- this.tips = tips;
16
+ let message = opts?.message || String(code);
17
+ const cause = opts?.cause;
18
+ super(message, { cause });
19
+ this.name = 'RouterError';
20
+ let codeNum = opts?.code || (typeof code === 'number' ? code : undefined);
21
+ this.code = codeNum ?? 500;
22
+ this.message = message!;
18
23
  // 这一步可不写,默认会保存堆栈追踪信息到自定义错误构造函数之前,
19
24
  // 而如果写成 `Error.captureStackTrace(this)` 则自定义错误的构造函数也会被保存到堆栈追踪信息
20
25
  Error.captureStackTrace(this, this.constructor);
@@ -31,8 +36,7 @@ export class CustomError extends Error {
31
36
  return {
32
37
  code: e?.code,
33
38
  data: e?.data,
34
- message: e?.message,
35
- tips: e?.tips,
39
+ message: e?.message
36
40
  };
37
41
  }
38
42
  /**
@@ -52,7 +56,6 @@ export class CustomError extends Error {
52
56
  code: e?.code,
53
57
  data: e?.data,
54
58
  message: e?.message,
55
- tips: e?.tips,
56
59
  };
57
60
  }
58
61
  }
package/src/route.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CustomError } from './result/error.ts';
1
+ import { CustomError, CustomErrorOptions } from './result/error.ts';
2
2
  import { pick } from './utils/pick.ts';
3
3
  import { listenProcess, MockProcess } from './utils/listen-process.ts';
4
4
  import { z } from 'zod';
@@ -610,9 +610,21 @@ export class QueryRouter {
610
610
  importRouter(router: QueryRouter) {
611
611
  this.importRoutes(router.routes);
612
612
  }
613
- throw(code?: number | string, message?: string, tips?: string): void;
613
+ throw(code?: number | string, message?: string): void;
614
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
615
+ throw(opts?: CustomErrorOptions): void;
614
616
  throw(...args: any[]) {
615
- throw new CustomError(...args);
617
+ const [args0, args1] = args;
618
+ if (args0 && typeof args0 === 'object') {
619
+ throw new CustomError(args0);
620
+ }
621
+ if (args1 && typeof args1 === 'object') {
622
+ throw new CustomError(args0, args1);
623
+ } else if (args1) {
624
+ throw new CustomError(args0, { message: args1 });
625
+ }
626
+ // args1 不存在;
627
+ throw new CustomError(args0);
616
628
  }
617
629
  hasRoute(path: string, key: string = '') {
618
630
  return this.routes.find((r) => r.path === path && r.key === key);
@@ -4,6 +4,7 @@ import * as cookie from './cookie.ts';
4
4
  import { ServerType, Listener, OnListener, ServerOpts, OnWebSocketOptions, OnWebSocketFn, WebSocketListenerFun, ListenerFun, HttpListenerFun, WS } from './server-type.ts';
5
5
  import { parseIfJson } from '../utils/parse.ts';
6
6
  import { EventEmitter } from 'eventemitter3';
7
+ import { CustomError } from '../result/error.ts';
7
8
  type CookieFn = (name: string, value: string, options?: cookie.SerializeOptions, end?: boolean) => void;
8
9
 
9
10
  export type HandleCtx = {
@@ -165,8 +166,13 @@ export class ServerBase implements ServerType {
165
166
  res.end(JSON.stringify(end));
166
167
  }
167
168
  } catch (e) {
168
- console.error(e);
169
169
  res.setHeader('Content-Type', 'application/json; charset=utf-8');
170
+ if (CustomError.isError(e)) {
171
+ const parsedError = CustomError.parseError(e);
172
+ res.end(JSON.stringify(parsedError));
173
+ return;
174
+ }
175
+ console.error(e);
170
176
  if (e.code && typeof e.code === 'number') {
171
177
  res.end(resultError(e.message || `Router Server error`, e.code));
172
178
  } else {