{"record":{"id":"90634ea00838a9aa","repo":"linshenkx/prompt-optimizer","slug":"failed-to-reorder-categories-errormessage","errorCode":null,"errorMessage":"Failed to reorder categories: ${errorMessage}","messagePattern":"Failed to reorder categories: (.+?)","errorType":"exception","errorClass":"FavoriteStorageError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/favorite/manager.ts","lineNumber":1184,"sourceCode":"              sortOrder: index\n            });\n            categoryMap.delete(id);\n          }\n        });\n\n        // 将未在ID列表中的分类追加到末尾\n        categoryMap.forEach(category => {\n          reorderedCategories.push({\n            ...category,\n            sortOrder: reorderedCategories.length\n          });\n        });\n\n        return reorderedCategories;\n      });\n    } catch (error) {\n      const errorMessage = error instanceof Error ? error.message : String(error);\n      throw new FavoriteStorageError(`Failed to reorder categories: ${errorMessage}`);\n    }\n  }\n\n  async getCategoryUsage(categoryId: string): Promise<number> {\n    try {\n      const favorites = await this.getFavorites({ categoryId });\n      return favorites.length;\n    } catch (error) {\n      const errorMessage = error instanceof Error ? error.message : String(error);\n      throw new FavoriteStorageError(`Failed to get category usage: ${errorMessage}`);\n    }\n  }\n\n  async importFavorites(data: string, options?: {\n    mergeStrategy?: 'skip' | 'overwrite' | 'merge';\n    categoryMapping?: Record<string, string>;\n  }): Promise<{\n    imported: number;","sourceCodeStart":1166,"sourceCodeEnd":1202,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/favorite/manager.ts#L1166-L1202","documentation":"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).","triggerScenarios":"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.","commonSituations":"Reordering with stale ids after concurrent deletion; storage quota/I-O failures; multi-tab edits producing divergent id lists.","solutions":["Refresh getCategories() and build the id list from current data before reordering","Check the wrapped message; if reorder logic threw, verify every id exists","Retry after resolving storage issues"],"exampleFix":"// before\nawait manager.reorderCategories(lastKnownOrder);\n\n// after\nconst current = await manager.getCategories();\nconst validIds = lastKnownOrder.filter(id => current.some(c => c.id === id));\nif (validIds.length) await manager.reorderCategories(validIds);","handlingStrategy":"validation","validationCode":"const current = await manager.getCategories();\nconst validIds = categoryIds.filter(id => current.some(c => c.id === id));\nif (validIds.length) await manager.reorderCategories(validIds);","typeGuard":"const isFavoriteStorageError = (e: unknown): e is FavoriteStorageError => e instanceof FavoriteStorageError;","tryCatchPattern":"try { await manager.reorderCategories(ids); } catch (e) { if (isFavoriteStorageError(e)) { /* refresh categories, rebuild id list, retry once */ } throw e; }","preventionTips":["Build reorder id lists from fresh getCategories() data","Re-validate ids after any concurrent deletion"],"tags":["storage","category","reorder","favorites"],"backgroundTag":"storage-write-failed","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}