linshenkx/prompt-optimizer · warning · FavoriteValidationError

Category already exists: ${category.name}

Error message

Category already exists: ${category.name}

What it means

Thrown by FavoriteManager.addCategory when a category with the same name already exists in storage. The library enforces unique category names, so attempting to add a duplicate is treated as a validation error (FavoriteValidationError) rather than silently merging.

Source

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

    }

    const now = Date.now();
    const id = `cat_${now}_${Math.random().toString(36).substr(2, 9)}`;

    const newCategory: FavoriteCategory = {
      ...category,
      id,
      createdAt: now,
      sortOrder: category.sortOrder || 0
    };

    try {
      await this.storageProvider.updateData(this.STORAGE_KEYS.CATEGORIES, (categories: FavoriteCategory[] | null) => {
        const categoriesList = categories || [];
        // 检查是否已存在同名分类
        const existing = categoriesList.find(c => c.name === category.name);
        if (existing) {
          throw new FavoriteValidationError(`Category already exists: ${category.name}`);
        }
        return [...categoriesList, newCategory];
      });

      return id;
    } catch (error) {
      if (error instanceof FavoriteError) {
        throw error;
      }
      const errorMessage = error instanceof Error ? error.message : String(error);
      throw new FavoriteStorageError(`Failed to add category: ${errorMessage}`);
    }
  }

  async updateCategory(id: string, updates: Partial<FavoriteCategory>): Promise<void> {
    try {
      await this.storageProvider.updateData(this.STORAGE_KEYS.CATEGORIES, (categories: FavoriteCategory[] | null) => {
        const categoriesList = categories || [];

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Check for an existing category by name first (getCategories()) and reuse it instead of adding
  2. When importing, skip or rename duplicate category names before calling importFavorites
  3. Wrap addCategory in try-catch for FavoriteValidationError and treat it as idempotent success

Example fix

// before
await manager.addCategory({ name: 'Work', icon: 'briefcase' });

// after
const existing = (await manager.getCategories()).find(c => c.name === 'Work');
if (!existing) {
  await manager.addCategory({ name: 'Work', icon: 'briefcase' });
}
Defensive patterns

Strategy: validation

Validate before calling

const existing = (await manager.getCategories()).find(c => c.name === newCategory.name);
if (existing) { /* reuse existing.id */ }

Type guard

const isDuplicateCategoryError = (e: unknown) =>
  e instanceof FavoriteValidationError && e.message.startsWith('Category already exists');

Try / catch

try { await manager.addCategory(cat); } catch (e) { if (isDuplicateCategoryError(e)) return existing?.id; throw e; }

Prevention

When it happens

Trigger: Calling addCategory({name: 'Work'}) when a category named 'Work' already exists; also triggered indirectly via ensureDefaultCategories or importFavorites when the imported/default data contains a name that collides with an existing category.

Common situations: Importing favorites from a backup into a store that already has default categories; running ensureDefaultCategories twice without deduplication; importing the same export file twice.

Related errors


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