linshenkx/prompt-optimizer · error · StorageError
write
write
Error message
Failed to set item: ${key} What it means
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).
Source
Thrown at packages/core/src/services/storage/dexieStorageProvider.ts:119
throw new StorageError(`Failed to get item: ${key}`, 'read');
}
}
/**
* 设置存储项
*/
async setItem(key: string, value: string): Promise<void> {
await this.initialize();
try {
await this.db.storage.put({
key,
value,
timestamp: Date.now()
});
} catch (error) {
console.error(`Failed to set storage item (${key}):`, error);
throw new StorageError(`Failed to set item: ${key}`, 'write');
}
}
/**
* 删除存储项
*/
async removeItem(key: string): Promise<void> {
await this.initialize();
try {
await this.db.storage.delete(key);
} catch (error) {
console.error(`Failed to remove storage item (${key}):`, error);
throw new StorageError(`Failed to remove item: ${key}`, 'delete');
}
}
/**View on GitHub (pinned to 3e677b1d9f)
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
Example fix
// before
await provider.setItem('cache', hugePayload);
// after
try { await provider.setItem('cache', hugePayload); }
catch (e) { if (e.code === 'write') await evictOldEntries(); else throw e; } Defensive patterns
Strategy: try-catch
Validate before calling
const safe = JSON.parse(JSON.stringify(value)); // strip non-cloneable parts before persisting await provider.setItem(key, safe);
Type guard
const isStorageWriteError = (e: unknown): e is StorageError => e instanceof StorageError && (e as StorageError).code === 'write';
Try / catch
try { await provider.setItem(key, value); } catch (e) { if (isStorageWriteError(e)) { await evictLRU(); await provider.setItem(key, value); } else throw e; } Prevention
- 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)
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- read
- delete
- Failed to clear storage
- favorites payload exceeds hard limit of ${FAVORITES_HARD_LIM
- Failed to perform atomic update after ${maxRetries} attempts
AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27).
Data as JSON: /api/errors/a7a7bdfa40c6788e.
Report an issue: GitHub.