Skip to content

Let openBox skip records it cannot decode instead of failing outright - #321

Open
LahaLuhem wants to merge 4 commits into
IO-Design-Team:mainfrom
LahaLuhem:bugfix/#318-throw-fails-open-box
Open

Let openBox skip records it cannot decode instead of failing outright#321
LahaLuhem wants to merge 4 commits into
IO-Design-Team:mainfrom
LahaLuhem:bugfix/#318-throw-fails-open-box

Conversation

@LahaLuhem

@LahaLuhem LahaLuhem commented Aug 23, 2026

Copy link
Copy Markdown

Fixes #318.

Right now one record whose adapter throws takes the whole box with it. openBox decodes every value before the box exists, so a single failure means no box at all and nothing else in there is reachable.

This adds an opt-in way to skip that record and keep the rest.

final box = await Hive.openBox<Thing>(
  'things',
  onUndecodableValue: (key, error, stackTrace) {
    log('dropping $key: $error');
  },
);

Leave it off and nothing changes. Pass it and bad records are skipped, with the key handed to you so you can log it, delete it or refetch it.

Nice side effect: throwing from inside the handler aborts the open. So you can skip a FormatException and still let a HiveError be fatal, which matters because a missing adapter registration and real corruption both surface as "unknown typeId" and only your app knows which it is. No second flag needed for that.

How it works

readFrame reads the frame length and checks the crc before it touches the value, so a record that fails to decode is structurally fine and its end is already known. Wrapping just the value-decode branch and falling back to Frame.deleted(key) drops that key, and the existing tail winds the reader to the exact frame end so everything after it still decodes.

Web goes through a different path with no frames at all, so it gets the same treatment around its per-value decode.

Not included, on purpose

Why
openLazyBox a lazy open only reads keys, and a bad record already fails at get, scoped to that one key
IsolatedHive it opens in verbatim mode and never runs adapters at open, so it was never affected

Confirmed the second one rather than assumed it:

verbatim:false -> THREW FormatException
verbatim:true  -> OK, keys=[a, b, c]

Testing

Six new tests. Four on the VM covering no-handler-still-throws, skip-and-report, throwing-aborts, and clean-box-never-called. Two on web built with encodeValue so the bytes really are undecodable.

Since these cover a new parameter, they cannot be red beforehand, they would not compile. So I broke the logic instead and checked the tests noticed.

What the mutations showed
Broke this Result
readFrame always rethrows both VM skip tests fail
web loop always rethrows web skip test fails
reverted the first commit web test mis-attributes, ['b','c'] instead of ['b']

That last one bit me for real while writing it. List.map's iterator sets its current value before bumping the index, so a throwing element gets retried and the failure lands on the wrong key. That is why the first commit exists.

One test passed under mutation at first, because when everything rethrows the open fails anyway. f1e25719 makes it count handler calls so it fails properly.

Ran 591 on the VM and 495 on chrome under both dart2js and dart2wasm. dart analyze --fatal-infos and dart format --set-exit-if-changed clean here and in hive_flutter, hive_generator and hive_ce_inspector. Also checked a lazy get on a bad record still throws, so the handler does not leak into the read path.

Needs more consideration

  1. StorageBackendJs has grown a fifth optional positional parameter, so the manager now passes TypeRegistryImpl.nullImpl explicitly. Named parameters would read better but that is a wider change to an internal class, happy to do it if you want.

  2. getValues(cursor: true) dartifies instead of decoding. Pre-existing and untouched, but my getRawValues sits next to it and makes the difference easier to spot. Looks like its own bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

One record whose adapter throws makes openBox fail, taking the whole box with it

1 participant