errors.ts 1.07 KB
import type { RequestConfig } from './facade';

/**
 * 带上下文的请求异常。
 *
 * 独立成模块是为了让 strategies.ts 能抛它而不必运行时反向 import facade
 * (facade 会 import strategies,循环依赖在 CJS 产物里会拿到 undefined)。
 * facade 原样 re-export,对 `@uj/api-core` 的公开 API 没有变化。
 */
export class RequestError extends Error {
  readonly isRequestError = true;
  code?: string | number;
  /** 业务失败信封里的 data 字段(web 老壳 `Object.assign(new Error(msg), { code, data })` 的语义)。 */
  data?: unknown;
  config?: RequestConfig;
  response?: unknown;
  cause?: unknown;

  constructor(message: string, init: Partial<RequestError> = {}) {
    super(message);
    this.name = 'RequestError';
    if (init.code !== undefined) this.code = init.code;
    if (init.data !== undefined) this.data = init.data;
    if (init.config !== undefined) this.config = init.config;
    if (init.response !== undefined) this.response = init.response;
    if (init.cause !== undefined) this.cause = init.cause;
  }
}