linshenkx/prompt-optimizer · error · StorageError
read
read
Error message
Failed to get item: ${key} What it means
StorageError with code 'read' thrown by DexieStorageProvider.getItem when the underlying Dexie table lookup (this.db.storage.get(key)) rejects. Dexie rejections typically stem from database being closed, schema mismatch, IndexedDB being blocked/unavailable, or quota/private-mode restrictions in the browser.
Source
Thrown at packages/core/src/services/storage/dexieStorageProvider.ts:101
* 重置迁移状态(主要用于测试)
*/
static resetMigrationState(): void {
// 因为迁移逻辑已移除,此函数不再需要
// 保留为空函数以避免破坏测试的API
}
/**
* 获取存储项
*/
async getItem(key: string): Promise<string | null> {
await this.initialize();
try {
const record = await this.db.storage.get(key);
return record?.value ?? null;
} catch (error) {
console.error(`Failed to get storage item (${key}):`, error);
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');View on GitHub (pinned to 3e677b1d9f)
Solutions
- Check console output — the original Dexie error is logged before wrapping
- Ensure the Dexie database is open and the schema version includes the 'storage' table; bump the version and provide an upgrade handler if the schema changed
- Retry after reopening the database (db.open()) or reloading the app; verify IndexedDB is available in the environment
Example fix
// before
const v = await provider.getItem('k');
// after
if (!provider.db.isOpen()) await provider.db.open();
const v = await provider.getItem('k').catch(e => { console.warn('read failed', e); return null; }); Defensive patterns
Strategy: try-catch
Validate before calling
if (!provider.db.isOpen()) await provider.db.open(); // or provider.initialize()
Type guard
const isStorageReadError = (e: unknown): e is StorageError => e instanceof StorageError && (e as StorageError).code === 'read';
Try / catch
try { return await provider.getItem(key); } catch (e) { if (isStorageReadError(e)) return null; /* missing/corrupt read treated as absent */ throw e; } Prevention
- Call initialize() once at app start and reuse the instance
- Bump Dexie schema versions with upgrade paths to avoid table-mismatch errors
- Log the original console.error output — it carries the true Dexie cause
When it happens
Trigger: Calling getItem(key) after the Dexie database was closed, when the 'storage' table doesn't exist due to a schema/version mismatch, or when IndexedDB access fails (private browsing, storage corrupted, deleted while open).
Common situations: App updated with a changed schema but old DB version present; running in a private/incognito window with restricted IndexedDB; another tab forced a version upgrade blocking this connection; user cleared storage while the app held it open.
Related errors
- write
- delete
- Failed to clear storage
- Failed to perform atomic update after ${maxRetries} attempts
- Failed to perform simple update: ${key}
AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27).
Data as JSON: /api/errors/4774c2f41bc0fe49.
Report an issue: GitHub.