Repository navigation
feat(persistence): report every persistence failure with its flow and operation - #14
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Persistence failures are hard to report reliably today:
createPersistercatches store errors itself and only calls its ownonError, soonPersistenceErroron<Flow>/FlowProvidernever hears about a failed localStorage write or AsyncStorage read.onPersistenceErrorgets only the error, with no way to tell which flow or operation failed.incompatible_stateis reported twice: once toonDiscardand again toonPersistenceError.kvStorageAdapter.getturns read failures and corrupted data intonull, so they look exactly like "nothing saved".After this change, one handler sees every failure, once, with context:
onPersistenceError(error, info):infois{ flowId, operation: "save" | "restore" | "remove", instanceId?, variantId? }. Handlers that only takeerrorkeep working.onErroron the persister:save(),restore()andremove()acceptonError, which replaces the persister'sonErrorfor that call (the same ruleonDiscardalready follows).<Flow>passes one wheneveronPersistenceErroris set, so failures caught inside the persister reach it and the persister'sonErrordoesn't also fire. Without a Flow/provider handler, the persister'sonErrorruns as before. Custom persisters should call the per-callonErrorwhen an operation fails.incompatible_statereported once: toonDiscardwhen one is set, otherwise toonPersistenceError.kvStorageAdapter.getthrows when the storage read fails or stored data can't be deserialized. The persister reports it and restores nothing, so the flow still starts fresh. This covers the localStorage, sessionStorage and AsyncStorage stores.list()still skips unreadable entries.New exported types:
FlowPersistenceErrorInfo,FlowPersistenceOperation,FlowPersistenceErrorHandler.Type of Change
Breaking for apps that set both
onPersistenceErrorand a persisteronError(the persister's no longer fires for those flows), apps that relied onincompatible_statereaching both callbacks, and custom stores built onkvStorageAdapterthat expectedgetnever to throw.Checklist
bun run test)bun run typecheck)bun run check)bun changeset)Testing
bun run test: 405 tests pass. Of the 14 new tests, 9 fail onmainand 5 pass on both (fallback and compatibility cases).onErrorreplaces the persister's for save, restore and remove; the persister'sonErroris still used when no per-call handler is given; the handler isn't forwarded to the store; a throwing handler doesn't reject; corrupted stored JSON reachesonErrorand restore returnsnull.createPersisterreaches the provider'sonPersistenceErrorwith{ flowId, operation: "restore", instanceId, variantId }, and the persister'sonErrordoesn't fire; save failures reportoperation: "save"; without a Flow handler the persister'sonErrorstill runs;incompatible_statewithoutonDiscardreachesonPersistenceErrorexactly once, and withonDiscardit doesn't reachonPersistenceError; a one-argument handler still type-checks.