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E,IAAI,CAAC,KAAK;YACjB,KAAK,EAAE,IAAI,CAAC,KAAK;gBACf,CAAC,CAAC;oBACE,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI;oBACrB,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO;oBAC3B,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK;iBACxB;gBACH,CAAC,CAAC,SAAS;SACd,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,QAAQ;QACN,IAAI,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,OAAO,EAAE,CAAC;QAE7C,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,IAAI,CAAC,IAAI,GAAG,CAAC;QAC9B,CAAC;QAED,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,kBAAkB,IAAI,CAAC,KAAK,EAAE,CAAC;QAC3C,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AArGD,wDAqGC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,wBAAwB,CACtC,KAAc;IAEd,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACb,KAA4B,CAAC,IAAI,KAAK,wBAAwB,CAChE,CAAC;AACJ,CAAC;AARD,4DAQC;AAED;;;;;;;GAOG;AACH,SAAgB,sBAAsB,CACpC,KAAc;IAEd,OAAO,CACL,wBAAwB,CAAC,KAAK,CAAC;QAC/B,KAAK,CAAC,OAAO,KAAK,yCAA6B,CAAC,eAAe,CAChE,CAAC;AACJ,CAAC;AAPD,wDAOC","sourcesContent":["import { KeyringControllerErrorMessage } from './constants';\n\n/**\n * Options for creating a KeyringControllerError.\n */\nexport type KeyringControllerErrorOptions = {\n  /**\n   * The underlying error that caused this error (for error chaining).\n   * Uses the standard Error.cause property (ES2022).\n   */\n  cause?: Error;\n  /**\n   * Optional error code for programmatic error handling.\n   * This can be used to identify specific error types without string matching.\n   */\n  code?: string;\n  /**\n   * Additional context data associated with the error.\n   * Useful for debugging and error reporting.\n   */\n  context?: Record<string, unknown>;\n};\n\n/**\n * Error class for KeyringController-related errors.\n *\n * This error class extends the standard Error class and supports:\n * - Error chaining via the `cause` property (ES2022 standard)\n * - Optional error codes for programmatic error handling\n * - Additional context data for debugging\n * - Backward compatibility with the legacy `originalError` property\n */\nexport class KeyringControllerError extends Error {\n  /**\n   * Optional error code for programmatic error handling.\n   */\n  code?: string;\n\n  /**\n   * Additional context data associated with the error.\n   */\n  context?: Record<string, unknown>;\n\n  /**\n   * The underlying error that caused this error (ES2022 standard).\n   * This is set manually for compatibility with older TypeScript versions.\n   */\n  cause?: Error;\n\n  /**\n   * @deprecated Use `cause` instead. This property is maintained for backward compatibility.\n   */\n  originalError?: Error;\n\n  /**\n   * Creates a new KeyringControllerError.\n   *\n   * @param message - The error message.\n   * @param options - Error options or an Error object for backward compatibility.\n   */\n  constructor(\n    message: string,\n    options?: KeyringControllerErrorOptions | Error,\n  ) {\n    super(message);\n    this.name = 'KeyringControllerError';\n\n    // Support both new signature (options object) and legacy signature (Error as second param)\n    const cause = options instanceof Error ? options : options?.cause;\n    const code = options instanceof Error ? undefined : options?.code;\n    const context = options instanceof Error ? undefined : options?.context;\n\n    // Set cause property for error chaining (ES2022 standard)\n    if (cause) {\n      this.cause = cause;\n      // Maintain backward compatibility with originalError\n      this.originalError = cause;\n    }\n\n    // Set code and data if provided\n    if (code) {\n      this.code = code;\n    }\n    if (context) {\n      this.context = context;\n    }\n\n    // Ensure proper prototype chain for instanceof checks\n    Object.setPrototypeOf(this, KeyringControllerError.prototype);\n  }\n\n  /**\n   * Returns a JSON representation of the error.\n   * Useful for logging and error reporting.\n   *\n   * @returns JSON representation of the error.\n   */\n  toJSON(): Record<string, unknown> {\n    return {\n      name: this.name,\n      message: this.message,\n      code: this.code,\n      context: this.context,\n      stack: this.stack,\n      cause: this.cause\n        ? {\n            name: this.cause.name,\n            message: this.cause.message,\n            stack: this.cause.stack,\n          }\n        : undefined,\n    };\n  }\n\n  /**\n   * Returns a string representation of the error chain.\n   * Includes all chained errors for better debugging.\n   *\n   * @returns String representation of the error chain.\n   */\n  toString(): string {\n    let result = `${this.name}: ${this.message}`;\n\n    if (this.code) {\n      result += ` [${this.code}]`;\n    }\n\n    if (this.cause) {\n      result += `\\n  Caused by: ${this.cause}`;\n    }\n\n    return result;\n  }\n}\n\n/**\n * Returns `true` if the error is a `KeyringControllerError`.\n *\n * Uses duck-typing on `error.name` rather than `instanceof` so that the check\n * remains correct when multiple major versions of `@metamask/keyring-controller`\n * coexist in the dependency tree (different versions produce different classes,\n * so `instanceof` would return `false` for errors from another version).\n *\n * @param error - The value to check.\n * @returns Whether the error is a `KeyringControllerError`.\n */\nexport function isKeyringControllerError(\n  error: unknown,\n): error is KeyringControllerError {\n  return (\n    typeof error === 'object' &&\n    error !== null &&\n    (error as { name?: unknown }).name === 'KeyringControllerError'\n  );\n}\n\n/**\n * Returns `true` if the error is a `KeyringNotFound` error thrown by\n * `KeyringController:withKeyring`. Use this to distinguish a missing keyring\n * from other failures and apply fallback logic.\n *\n * @param error - The value to check.\n * @returns Whether the error is a `KeyringNotFound` error.\n */\nexport function isKeyringNotFoundError(\n  error: unknown,\n): error is KeyringControllerError {\n  return (\n    isKeyringControllerError(error) &&\n    error.message === KeyringControllerErrorMessage.KeyringNotFound\n  );\n}\n"]}