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 }