Skip to content

Commit 4027b8d

Browse files
GarmashAlexyihuangAlex | Interchain Labs
authored
fix: properly store errors in Iterator.Next() method (#1090)
Co-authored-by: yihuang <huang@crypto.com> Co-authored-by: Alex | Interchain Labs <alex@interchainlabs.io>
1 parent f05dd3a commit 4027b8d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

iterator.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,11 @@ func (iter *Iterator) Next() {
228228
}
229229

230230
node, err := iter.t.next()
231-
// TODO: double-check if this error is correctly handled.
231+
// If an error occurred or no more nodes, update iterator state accordingly
232232
if node == nil || err != nil {
233+
if err != nil {
234+
iter.err = err
235+
}
233236
iter.t = nil
234237
iter.valid = false
235238
return

iterator_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,31 @@ func syncMapCount(m *sync.Map) int {
381381
})
382382
return count
383383
}
384+
385+
func TestIterator_Next_ErrorHandling(t *testing.T) {
386+
db := dbm.NewMemDB()
387+
ndb := newNodeDB(db, 0, DefaultOptions(), NewNopLogger())
388+
tree := &ImmutableTree{ndb: ndb}
389+
390+
// Create a branch node with a left child key that does not exist in the DB
391+
node := &Node{
392+
key: []byte("err"),
393+
leftNodeKey: []byte("missing"), // triggers GetNode error
394+
rightNodeKey: nil,
395+
subtreeHeight: 1,
396+
}
397+
tree.root = node
398+
399+
iter := &Iterator{
400+
start: nil,
401+
end: nil,
402+
valid: true,
403+
t: node.newTraversal(tree, nil, nil, true, false, false),
404+
}
405+
406+
iter.Next()
407+
408+
require.False(t, iter.Valid(), "iterator should be invalid after error")
409+
require.Error(t, iter.Error(), "iterator should have error set")
410+
require.Contains(t, iter.Error().Error(), "node does not have a nodeKey")
411+
}

0 commit comments

Comments
 (0)