From a8f409f900f70c7f2ce67ebe18d39488a97a4417 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Fri, 20 Feb 2026 22:53:03 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7=E8=87=B30.0.82=EF=BC=8C=E5=B9=B6=E5=9C=A8=20CustomErr?= =?UTF-8?q?or=20=E7=B1=BB=E4=B8=AD=E5=A2=9E=E5=BC=BA=E6=9E=84=E9=80=A0?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E4=BB=A5=E6=94=AF=E6=8C=81=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/result/error.ts | 8 ++++++-- src/route.ts | 18 +++++++++++++++--- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index e203e8c..5297a58 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "$schema": "https://json.schemastore.org/package", "name": "@kevisual/router", - "version": "0.0.81", + "version": "0.0.82", "description": "", "type": "module", "main": "./dist/router.js", diff --git a/src/result/error.ts b/src/result/error.ts index 12d320e..975c636 100644 --- a/src/result/error.ts +++ b/src/result/error.ts @@ -1,4 +1,4 @@ -type CustomErrorOptions = { +export type CustomErrorOptions = { cause?: Error | string; code?: number; message?: string; @@ -8,7 +8,11 @@ export class CustomError extends Error { code?: number; data?: any; 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); const cause = opts?.cause; super(message, { cause }); diff --git a/src/route.ts b/src/route.ts index f24035c..b58074b 100644 --- a/src/route.ts +++ b/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 { listenProcess, MockProcess } from './utils/listen-process.ts'; import { z } from 'zod'; @@ -610,9 +610,21 @@ export class QueryRouter { importRouter(router: QueryRouter) { 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 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 = '') { return this.routes.find((r) => r.path === path && r.key === key);