chore: 更新版本号至0.0.82,并在 CustomError 类中增强构造函数以支持对象参数
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://json.schemastore.org/package",
|
"$schema": "https://json.schemastore.org/package",
|
||||||
"name": "@kevisual/router",
|
"name": "@kevisual/router",
|
||||||
"version": "0.0.81",
|
"version": "0.0.82",
|
||||||
"description": "",
|
"description": "",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "./dist/router.js",
|
"main": "./dist/router.js",
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
type CustomErrorOptions = {
|
export type CustomErrorOptions = {
|
||||||
cause?: Error | string;
|
cause?: Error | string;
|
||||||
code?: number;
|
code?: number;
|
||||||
message?: string;
|
message?: string;
|
||||||
@@ -8,7 +8,11 @@ export class CustomError extends Error {
|
|||||||
code?: number;
|
code?: number;
|
||||||
data?: any;
|
data?: any;
|
||||||
message: string;
|
message: string;
|
||||||
constructor(code?: number | string, opts?: CustomErrorOptions) {
|
constructor(code?: number | string | CustomErrorOptions, opts?: CustomErrorOptions) {
|
||||||
|
if (typeof code === 'object' && code !== null) {
|
||||||
|
opts = code;
|
||||||
|
code = opts.code || 500;
|
||||||
|
}
|
||||||
let message = opts?.message || String(code);
|
let message = opts?.message || String(code);
|
||||||
const cause = opts?.cause;
|
const cause = opts?.cause;
|
||||||
super(message, { cause });
|
super(message, { cause });
|
||||||
|
|||||||
18
src/route.ts
18
src/route.ts
@@ -1,4 +1,4 @@
|
|||||||
import { CustomError } from './result/error.ts';
|
import { CustomError, CustomErrorOptions } from './result/error.ts';
|
||||||
import { pick } from './utils/pick.ts';
|
import { pick } from './utils/pick.ts';
|
||||||
import { listenProcess, MockProcess } from './utils/listen-process.ts';
|
import { listenProcess, MockProcess } from './utils/listen-process.ts';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
@@ -610,9 +610,21 @@ export class QueryRouter {
|
|||||||
importRouter(router: QueryRouter) {
|
importRouter(router: QueryRouter) {
|
||||||
this.importRoutes(router.routes);
|
this.importRoutes(router.routes);
|
||||||
}
|
}
|
||||||
throw(code?: number | string, message?: string, tips?: string): void;
|
throw(code?: number | string, message?: string): void;
|
||||||
|
throw(code?: number | string, opts?: CustomErrorOptions): void;
|
||||||
|
throw(opts?: CustomErrorOptions): void;
|
||||||
throw(...args: any[]) {
|
throw(...args: any[]) {
|
||||||
throw new CustomError(...args);
|
const [args0, args1] = args;
|
||||||
|
if (args0 && typeof args0 === 'object') {
|
||||||
|
throw new CustomError(args0);
|
||||||
|
}
|
||||||
|
if (args1 && typeof args1 === 'object') {
|
||||||
|
throw new CustomError(args0, args1);
|
||||||
|
} else if (args1) {
|
||||||
|
throw new CustomError(args0, { message: args1 });
|
||||||
|
}
|
||||||
|
// args1 不存在;
|
||||||
|
throw new CustomError(args0);
|
||||||
}
|
}
|
||||||
hasRoute(path: string, key: string = '') {
|
hasRoute(path: string, key: string = '') {
|
||||||
return this.routes.find((r) => r.path === path && r.key === key);
|
return this.routes.find((r) => r.path === path && r.key === key);
|
||||||
|
|||||||
Reference in New Issue
Block a user