{"record":{"id":"a7a7bdfa40c6788e","repo":"linshenkx/prompt-optimizer","slug":"write-a7a7bd","errorCode":"write","errorMessage":"Failed to set item: ${key}","messagePattern":"Failed to set item: (.+?)","errorType":"exception","errorClass":"StorageError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/storage/dexieStorageProvider.ts","lineNumber":119,"sourceCode":"      throw new StorageError(`Failed to get item: ${key}`, 'read');\n    }\n  }\n\n  /**\n   * 设置存储项\n   */\n  async setItem(key: string, value: string): Promise<void> {\n    await this.initialize();\n    \n    try {\n      await this.db.storage.put({\n        key,\n        value,\n        timestamp: Date.now()\n      });\n    } catch (error) {\n      console.error(`Failed to set storage item (${key}):`, error);\n      throw new StorageError(`Failed to set item: ${key}`, 'write');\n    }\n  }\n\n  /**\n   * 删除存储项\n   */\n  async removeItem(key: string): Promise<void> {\n    await this.initialize();\n    \n    try {\n      await this.db.storage.delete(key);\n    } catch (error) {\n      console.error(`Failed to remove storage item (${key}):`, error);\n      throw new StorageError(`Failed to remove item: ${key}`, 'delete');\n    }\n  }\n\n  /**","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/storage/dexieStorageProvider.ts#L101-L137","documentation":"StorageError with code 'write' thrown by DexieStorageProvider.setItem when db.storage.put rejects. Common root causes are storage quota exhaustion, the database being closed, schema mismatch, or the value failing IndexedDB structured-clone (e.g., containing functions).","triggerScenarios":"Calling setItem with a very large value past the browser quota, a non-cloneable value (function, DOM node), or after the Dexie connection was closed/blocked by a version change in another tab.","commonSituations":"Persisting large JSON blobs (caches, logs) until quota is exceeded; storing class instances with methods; multi-tab app where one tab upgraded the DB version.","solutions":["Check the logged Dexie error; if QuotaExceededError, trim data or evict old keys before writing","Ensure the value is plain serializable data (the provider JSON.stringifies, so pass JSON-safe objects and verify key exists in schema)","Catch the error and apply a fallback (memory storage) or prompt the user to free space"],"exampleFix":"// before\nawait provider.setItem('cache', hugePayload);\n\n// after\ntry { await provider.setItem('cache', hugePayload); }\ncatch (e) { if (e.code === 'write') await evictOldEntries(); else throw e; }","handlingStrategy":"try-catch","validationCode":"const safe = JSON.parse(JSON.stringify(value)); // strip non-cloneable parts before persisting\nawait provider.setItem(key, safe);","typeGuard":"const isStorageWriteError = (e: unknown): e is StorageError =>\n  e instanceof StorageError && (e as StorageError).code === 'write';","tryCatchPattern":"try { await provider.setItem(key, value); } catch (e) { if (isStorageWriteError(e)) { await evictLRU(); await provider.setItem(key, value); } else throw e; }","preventionTips":["Cap the size of values written; compress or chunk large payloads","Periodically estimate storage usage (navigator.storage.estimate()) and evict proactively","Keep persisted objects plain (no functions/DOM references)"],"tags":["storage","indexeddb","quota","dexie"],"backgroundTag":"storage-write-failed","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}