From 59d398b1ca4f872afef0d3e58bad1fe2eb57b07c Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Sat, 15 Apr 2023 10:21:54 +0800 Subject: [PATCH] trie: print error log instead of panic directly --- trie/stacktrie.go | 7 ++++--- trie/trie.go | 21 +++++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/trie/stacktrie.go b/trie/stacktrie.go index 30bb12855696..b42c6a1b0ade 100644 --- a/trie/stacktrie.go +++ b/trie/stacktrie.go @@ -21,12 +21,12 @@ import ( "bytes" "encoding/gob" "errors" - "fmt" "io" "sync" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/log" ) var ErrCommitDisabled = errors.New("no database for committing") @@ -212,10 +212,11 @@ func (st *StackTrie) Update(key, value []byte) error { return nil } -// MustUpdate is a wrapper of Update and panic if any error is encountered. +// MustUpdate is a wrapper of Update and will omit any encountered error but +// just print out an error message. func (st *StackTrie) MustUpdate(key, value []byte) { if err := st.Update(key, value); err != nil { - panic(fmt.Errorf("unexpected error in StackTrie.Update: %w", err)) + log.Error("Unhandled trie error in StackTrie.Update", "err", err) } } diff --git a/trie/trie.go b/trie/trie.go index 44a30935589f..2e9d2b2c2ad4 100644 --- a/trie/trie.go +++ b/trie/trie.go @@ -24,6 +24,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/log" ) // Trie is a Merkle Patricia Trie. Use New to create a trie that sits on @@ -104,11 +105,12 @@ func (t *Trie) NodeIterator(start []byte) NodeIterator { return newNodeIterator(t, start) } -// MustGet is a wrapper of Get and panic if any error is encountered. +// MustGet is a wrapper of Get and will omit any encountered error but just +// print out an error message. func (t *Trie) MustGet(key []byte) []byte { res, err := t.Get(key) if err != nil { - panic(fmt.Errorf("unexpected error in trie.Get: %w", err)) + log.Error("Unhandled trie error in Trie.Get", "err", err) } return res } @@ -162,11 +164,12 @@ func (t *Trie) get(origNode node, key []byte, pos int) (value []byte, newnode no } } -// MustGetNode is a wrapper of GetNode and panic if any error is encountered. +// MustGetNode is a wrapper of GetNode and will omit any encountered error but +// just print out an error message. func (t *Trie) MustGetNode(path []byte) ([]byte, int) { item, resolved, err := t.GetNode(path) if err != nil { - panic(fmt.Errorf("unexpected error in tre.GetNode: %w", err)) + log.Error("Unhandled trie error in Trie.GetNode", "err", err) } return item, resolved } @@ -251,11 +254,12 @@ func (t *Trie) getNode(origNode node, path []byte, pos int) (item []byte, newnod } } -// MustUpdate is a wrapper of Update and panic if any error is encountered. +// MustUpdate is a wrapper of Update and will omit any encountered error but +// just print out an error message. func (t *Trie) MustUpdate(key, value []byte) { err := t.Update(key, value) if err != nil { - panic(fmt.Errorf("unexpected error in trie.Update: %w", err)) + log.Error("Unhandled trie error in Trie.Update", "err", err) } } @@ -370,11 +374,12 @@ func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error } } -// MustDelete is a wrapper of Delete and panic if any error is encountered. +// MustDelete is a wrapper of Delete and will omit any encountered error but +// just print out an error message. func (t *Trie) MustDelete(key []byte) { err := t.Delete(key) if err != nil { - panic(fmt.Errorf("unexpected error in trie.Delete: %w", err)) + log.Error("Unhandled trie error in Trie.Delete", "err", err) } }