{"record":{"id":"ce000bfbfbbecb33","repo":"linshenkx/prompt-optimizer","slug":"failed-to-get-category-usage-errormessage","errorCode":null,"errorMessage":"Failed to get category usage: ${errorMessage}","messagePattern":"Failed to get category usage: (.+?)","errorType":"exception","errorClass":"FavoriteStorageError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/favorite/manager.ts","lineNumber":1194,"sourceCode":"            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;\n    skipped: number;\n    errors: string[];\n  }> {\n    const mergeStrategy = options?.mergeStrategy || 'skip';\n    const categoryMapping = options?.categoryMapping || {};\n    const result = { imported: 0, skipped: 0, errors: [] as string[] };\n\n    try {\n      await this.ensureInitialized();\n      const importData = JSON.parse(data);","sourceCodeStart":1176,"sourceCodeEnd":1212,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/favorite/manager.ts#L1176-L1212","documentation":"Thrown by FavoriteManager.getCategoryUsage when this.getFavorites({ categoryId }) throws. The original error's message is captured into a FavoriteStorageError with the 'Failed to get category usage' prefix; this is a persistence-layer failure wrapper, not an input validation error.","triggerScenarios":"Calling getCategoryUsage(categoryId) before ensureInitialized has completed, when the underlying storage backend throws (quota, corruption, permission), or when getFavorites fails for any reason while counting favorites in a category.","commonSituations":"Browser storage disabled or quota exceeded, storage corrupted after a schema/version change, calling the manager concurrently during initialization, or running in SSR/headless environments without persistent storage.","solutions":["Inspect the wrapped message (and the original error) to identify the underlying storage failure — it is appended after the colon","Ensure FavoriteManager is fully initialized (ensureInitialized) before calling getCategoryUsage","Check browser storage availability and quota (e.g. navigator.storage.estimate()) if quota issues are suspected","If storage is corrupted, export what is recoverable, reset the favorites store, and re-import"],"exampleFix":"// before\nconst usage = await manager.getCategoryUsage(catId);\n\n// after\ntry {\n  const usage = await manager.getCategoryUsage(catId);\n} catch (e) {\n  if (e instanceof FavoriteStorageError) {\n    console.error('storage failure:', e.message);\n  }\n}","handlingStrategy":"try-catch","validationCode":"await manager.ensureInitialized(); // make sure storage is ready before usage queries","typeGuard":"const isFavoriteStorageError = (e: unknown): e is FavoriteStorageError =>\n  e instanceof Error && e.name === 'FavoriteStorageError';","tryCatchPattern":"try {\n  const usage = await manager.getCategoryUsage(categoryId);\n} catch (e) {\n  if (isFavoriteStorageError(e)) {\n    // message suffix carries the underlying storage error; degrade gracefully\n    return 0;\n  }\n  throw e;\n}","preventionTips":["Initialize the FavoriteManager before querying category usage","Feature-detect browser storage availability at startup","Log the wrapped message to diagnose the real storage failure"],"tags":["storage","favorites","category","persistence","wrapper"],"backgroundTag":"storage-backend-unavailable","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}