From 9ca4608fe092e6231941ae6856128d283e3e3a21 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Fri, 15 May 2026 15:40:15 +0800 Subject: [PATCH] feat: add ApiError class to preserve error data through unwrapApiBody Co-Authored-By: Claude Opus 4.7 --- app/utils/http/factory.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/utils/http/factory.ts b/app/utils/http/factory.ts index ec09001..3e77bc0 100644 --- a/app/utils/http/factory.ts +++ b/app/utils/http/factory.ts @@ -7,13 +7,20 @@ export type ApiResponse = { data: T } +/** Wraps API error responses, preserving the data field for field-level error handling */ +export class ApiError extends Error { + constructor(message: string, public data: unknown) { + super(message) + this.name = 'ApiError' + } +} /** 从 Nitro 推断的响应体上剥离一层 `ApiResponse`,得到 `data` 字段类型 */ export type UnwrapApiResponse = T extends ApiResponse ? D : T export function unwrapApiBody(payload: ApiResponse): T { if (payload.code !== 0) { - throw new Error(payload.message) + throw new ApiError(payload.message, payload.data) } return payload.data }