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
- Check for an existing category by name first (getCategories()) and reuse it instead of adding
- When importing, skip or rename duplicate category names before calling importFavorites
- 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
- Deduplicate category names before importFavorites
- Make addCategory flows idempotent by checking names first
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
- Category ID list cannot be empty
- CATEGORY_ALREADY_EXISTS
- Cannot delete the last prompt asset version
- Cannot delete the current prompt asset version
- Category not found: ${id}
AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27).
Data as JSON: /api/errors/3bc4da42371d1187.
Report an issue: GitHub.