{"version":3,"file":"error.mjs","names":[],"sources":["../src/error.ts"],"sourcesContent":["import type { ErrorOptions } from './types'\nimport { colors, isServer } from './utils'\n\n/**\n * Structured error with context for better debugging\n *\n * @example\n * ```ts\n * throw new MxllogError({\n *   message: 'Failed to sync repository',\n *   status: 503,\n *   why: 'GitHub API rate limit exceeded',\n *   fix: 'Wait 1 hour or use a different token',\n *   link: 'https://docs.github.com/en/rest/rate-limit',\n *   cause: originalError,\n * })\n * ```\n */\nexport class MxllogError extends Error {\n\n  /** HTTP status code */\n  readonly status: number\n  readonly why?: string\n  readonly fix?: string\n  readonly link?: string\n\n  constructor(options: ErrorOptions | string) {\n    const opts = typeof options === 'string' ? { message: options } : options\n\n    super(opts.message, { cause: opts.cause })\n\n    this.name = 'MxllogError'\n    this.status = opts.status ?? 500\n    this.why = opts.why\n    this.fix = opts.fix\n    this.link = opts.link\n\n    // Maintain proper stack trace in V8\n    if (Error.captureStackTrace) {\n      Error.captureStackTrace(this, MxllogError)\n    }\n  }\n\n  /** HTTP status text (alias for message) */\n  get statusText(): string {\n    return this.message\n  }\n\n  /** HTTP status code (alias for compatibility) */\n  get statusCode(): number {\n    return this.status\n  }\n\n  /** HTTP status message (alias for compatibility) */\n  get statusMessage(): string {\n    return this.message\n  }\n\n  /** Structured data for serialization */\n  get data(): { why?: string, fix?: string, link?: string } | undefined {\n    if (this.why || this.fix || this.link) {\n      return { why: this.why, fix: this.fix, link: this.link }\n    }\n    return undefined\n  }\n\n  override toString(): string {\n    const useColors = isServer()\n\n    const red = useColors ? colors.red : ''\n    const yellow = useColors ? colors.yellow : ''\n    const cyan = useColors ? colors.cyan : ''\n    const dim = useColors ? colors.dim : ''\n    const reset = useColors ? colors.reset : ''\n    const bold = useColors ? colors.bold : ''\n\n    const lines: string[] = []\n\n    lines.push(`${red}${bold}Error:${reset} ${this.message}`)\n\n    if (this.why) {\n      lines.push(`${yellow}Why:${reset} ${this.why}`)\n    }\n\n    if (this.fix) {\n      lines.push(`${cyan}Fix:${reset} ${this.fix}`)\n    }\n\n    if (this.link) {\n      lines.push(`${dim}More info:${reset} ${this.link}`)\n    }\n\n    if (this.cause) {\n      lines.push(`${dim}Caused by:${reset} ${(this.cause as Error).message}`)\n    }\n\n    return lines.join('\\n')\n  }\n\n  toJSON(): Record<string, unknown> {\n    const { data } = this\n    return {\n      name: this.name,\n      message: this.message,\n      status: this.status,\n      ...(data && { data }),\n      ...(this.cause instanceof Error && {\n        cause: { name: this.cause.name, message: this.cause.message },\n      }),\n    }\n  }\n\n}\n\n/**\n * Create a structured error with context for debugging and user-facing messages.\n *\n * @param options - Error message string or full options object\n * @returns MxllogError with HTTP metadata (`status`, `statusText`) and `data`; also includes `statusCode` and `statusMessage` for legacy compatibility\n *\n * @example\n * ```ts\n * // Simple error\n * throw createError('Something went wrong')\n *\n * // Structured error with context\n * throw createError({\n *   message: 'Payment failed',\n *   status: 402,\n *   why: 'Card declined by issuer',\n *   fix: 'Try a different payment method',\n *   link: 'https://docs.example.com/payments',\n * })\n * ```\n */\nexport function createError(options: ErrorOptions | string): MxllogError {\n  return new MxllogError(options)\n}\n\nexport { createError as createMxllogError }\n"],"mappings":";;;;;;;;;;;;;;;;;AAkBA,IAAa,cAAb,MAAa,oBAAoB,MAAM;;CAGrC;CACA;CACA;CACA;CAEA,YAAY,SAAgC;EAC1C,MAAM,OAAO,OAAO,YAAY,WAAW,EAAE,SAAS,SAAS,GAAG;AAElE,QAAM,KAAK,SAAS,EAAE,OAAO,KAAK,OAAO,CAAC;AAE1C,OAAK,OAAO;AACZ,OAAK,SAAS,KAAK,UAAU;AAC7B,OAAK,MAAM,KAAK;AAChB,OAAK,MAAM,KAAK;AAChB,OAAK,OAAO,KAAK;AAGjB,MAAI,MAAM,kBACR,OAAM,kBAAkB,MAAM,YAAY;;;CAK9C,IAAI,aAAqB;AACvB,SAAO,KAAK;;;CAId,IAAI,aAAqB;AACvB,SAAO,KAAK;;;CAId,IAAI,gBAAwB;AAC1B,SAAO,KAAK;;;CA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