// services/api/src/api-exception.ts —— 携带契约错误码的业务异常(Task 7 遗留项①)。 // 状态码表(过滤器内 STATUS_TO_ERROR_CODE)只能表达"一个 HTTP 状态 = 一个错误码", // 无法表达同状态多错误码的契约空间(如 401 既可能是 AUTH_TOKEN_EXPIRED 也可能是 // AUTH_INVALID_CREDENTIALS,409 既可能是 MEMBER_CONFLICT 也可能是 // UPLOAD_SESSION_CONFLICT)。业务代码抛 ApiException 精确指定 ErrorCode; // 过滤器优先识别它,状态码表仅作普通 HttpException 的兜底映射。 import { HttpException } from '@nestjs/common'; import type { ErrorCode } from '@photo/contracts'; export class ApiException extends HttpException { constructor( readonly apiCode: ErrorCode, message: string, httpStatus: number, readonly details?: unknown, ) { // 响应体形状 {code, message, details?}:details 缺省时不占位(保持信封干净)。 // 注意 message 须从 getResponse().message 提取(见过滤器 extractMessage), // HttpException.message 属性在对象响应下不含业务文案。 super( details === undefined ? { code: apiCode, message } : { code: apiCode, message, details }, httpStatus, ); this.name = ApiException.name; } }