linshenkx/prompt-optimizer · error · FavoriteStorageError

Failed to reorder categories: ${errorMessage}

Error message

Failed to reorder categories: ${errorMessage}

What it means

FavoriteStorageError thrown by reorderCategories when the storage update that rewrites the category list in the new order fails, or when the reorder callback throws (e.g., an id in the list doesn't match any category and the callback assumes existence).

Source

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

              sortOrder: index
            });
            categoryMap.delete(id);
          }
        });

        // 将未在ID列表中的分类追加到末尾
        categoryMap.forEach(category => {
          reorderedCategories.push({
            ...category,
            sortOrder: reorderedCategories.length
          });
        });

        return reorderedCategories;
      });
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : String(error);
      throw new FavoriteStorageError(`Failed to reorder categories: ${errorMessage}`);
    }
  }

  async getCategoryUsage(categoryId: string): Promise<number> {
    try {
      const favorites = await this.getFavorites({ categoryId });
      return favorites.length;
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : String(error);
      throw new FavoriteStorageError(`Failed to get category usage: ${errorMessage}`);
    }
  }

  async importFavorites(data: string, options?: {
    mergeStrategy?: 'skip' | 'overwrite' | 'merge';
    categoryMapping?: Record<string, string>;
  }): Promise<{
    imported: number;

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Refresh getCategories() and build the id list from current data before reordering
  2. Check the wrapped message; if reorder logic threw, verify every id exists
  3. Retry after resolving storage issues

Example fix

// before
await manager.reorderCategories(lastKnownOrder);

// after
const current = await manager.getCategories();
const validIds = lastKnownOrder.filter(id => current.some(c => c.id === id));
if (validIds.length) await manager.reorderCategories(validIds);
Defensive patterns

Strategy: validation

Validate before calling

const current = await manager.getCategories();
const validIds = categoryIds.filter(id => current.some(c => c.id === id));
if (validIds.length) await manager.reorderCategories(validIds);

Type guard

const isFavoriteStorageError = (e: unknown): e is FavoriteStorageError => e instanceof FavoriteStorageError;

Try / catch

try { await manager.reorderCategories(ids); } catch (e) { if (isFavoriteStorageError(e)) { /* refresh categories, rebuild id list, retry once */ } throw e; }

Prevention

When it happens

Trigger: updateData failing on write; passing ids that don't correspond to stored categories causing the reorder logic inside the callback to throw a non-FavoriteError.

Common situations: Reordering with stale ids after concurrent deletion; storage quota/I-O failures; multi-tab edits producing divergent id lists.

Related errors


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