Skip to content

feat(useStorage): onError hook so consumers can surface failed writes #756

Description

@johnleider

Problem

createStorage's deep write watcher catches adapter errors and routes them to the internal logger without rethrowing (useStorage/index.tswriteStored catch → logger.error('[v0:storage] Failed to write key …')). Consumers have no programmatic way to learn that a write failed: storage.set cannot reject and no error state is exposed.

A full localStorage quota, a SecurityError in a restricted context, or an adapter-level failure is invisible to the app — the user keeps editing, believing their data is persisted, and nothing is.

Motivation

The Framework Builder (#241) added an isSaving/saveError contract to its store, with a visible "Could not save your changes" notice. Verified empirically: patching localStorage.setItem to throw produces only the internal [v0:storage] Failed to write key console log — the app-level catch is unreachable, so the notice can never fire for the localStorage backend. Any consumer that wants save-failure UX has the same dead end.

Proposal

An opt-in error hook on createStorage / createStoragePlugin:

createStoragePlugin({
  onError: (error, key) => { /* surface to app state */ },
})

Alternatives considered:

  • Rethrow from the watcher — breaking (writes are fire-and-forget deep-watcher side effects; a throw there lands in Vue's unhandled zone, not at a call site).
  • An error ref on the storage context — viable, but a callback composes better with per-consumer handling and matches the adapter-injection style used elsewhere.

The logger call can stay; the hook is additive.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions