linshenkx/prompt-optimizer · error · FavoriteImportExportError

Failed to export favorites: ${errorMessage}

Error message

Failed to export favorites: ${errorMessage}

What it means

FavoriteImportExportError thrown by exportFavorites when serializing the export payload (favorites, categories, tags) to JSON fails or any preceding read fails. The original error is attached as the cause.

Source

Thrown at packages/core/src/services/favorite/manager.ts:865

      } else {
        favorites = await this.getFavorites();
      }

      const categories = await this.getCategories();
      const tags = await this.getAllIndependentTags();

      const exportData = {
        version: '1.0',
        exportDate: new Date().toISOString(),
        favorites,
        categories,
        tags  // 导出独立标签库(包含所有标签:使用中的 + 预创建的)
      };

      return JSON.stringify(exportData, null, 2);
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : String(error);
      throw new FavoriteImportExportError(
        `Failed to export favorites: ${errorMessage}`,
        error instanceof Error ? error : undefined
      );
    }
  }

  /**
   * 计算标签使用统计
   * @private
   * @returns 包含标签名和使用次数的 Map
   */
  private async computeTagCounts(): Promise<Map<string, number>> {
    // 1. 获取独立标签
    const storedTags = await this.storageProvider.getItem(this.STORAGE_KEYS.TAGS);
    const independentTags: FavoriteTag[] = storedTags ? JSON.parse(storedTags) : [];

    // 2. 统计收藏项中使用的标签
    const favorites = await this.getFavorites();

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Inspect error.cause for the underlying failure
  2. Sanitize custom metadata (strip circular refs) before storing on favorites
  3. Export in smaller batches if memory is the issue
Defensive patterns

Strategy: try-catch

Type guard

const isImportExportError = (e: unknown): e is FavoriteImportExportError => e instanceof FavoriteImportExportError;

Try / catch

try { return await manager.exportFavorites(); } catch (e) { if (isImportExportError(e) && e.cause instanceof TypeError) { /* strip circular refs from metadata and retry */ } throw e; }

Prevention

When it happens

Trigger: JSON.stringify throwing on circular references in favorite data; getFavorites/getCategories/getTags failing during export assembly.

Common situations: User-defined metadata attached to favorites containing circular structures; storage read failures; very large datasets hitting memory limits.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/32d877e6b0f031c9. Report an issue: GitHub.