Skip to content

Commit

Permalink
chore(all): use errors.Is instead of err == (#2332)
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Mar 3, 2022
1 parent 666795b commit be078e5
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 8 deletions.
5 changes: 3 additions & 2 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package core

import (
"context"
"errors"
"math/big"
"sync"

Expand Down Expand Up @@ -204,9 +205,9 @@ func (s *Service) handleBlock(block *types.Block, state *rtstorage.TrieState) er

// store block in database
if err = s.blockState.AddBlock(block); err != nil {
if err == blocktree.ErrParentNotFound && block.Header.Number.Cmp(big.NewInt(0)) != 0 {
if errors.Is(err, blocktree.ErrParentNotFound) && block.Header.Number.Cmp(big.NewInt(0)) != 0 {
return err
} else if err == blocktree.ErrBlockExists || block.Header.Number.Cmp(big.NewInt(0)) == 0 {
} else if errors.Is(err, blocktree.ErrBlockExists) || block.Header.Number.Cmp(big.NewInt(0)) == 0 {
// this is fine
} else {
return err
Expand Down
2 changes: 1 addition & 1 deletion dot/state/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (s *GrandpaState) GetSetIDByBlockNumber(num *big.Int) (uint64, error) {

for {
changeUpper, err := s.GetSetIDChange(curr + 1)
if err == chaindb.ErrKeyNotFound {
if errors.Is(err, chaindb.ErrKeyNotFound) {
if curr == 0 {
return 0, nil
}
Expand Down
3 changes: 2 additions & 1 deletion dot/state/pruner/pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package pruner

import (
"errors"
"fmt"
"sync"
"time"
Expand Down Expand Up @@ -355,7 +356,7 @@ func (p *FullNode) storeLastPrunedIndex(blockNum int64) error {

func (p *FullNode) getLastPrunedIndex() (int64, error) {
val, err := p.journalDB.Get([]byte(lastPrunedKey))
if err == chaindb.ErrKeyNotFound {
if errors.Is(err, chaindb.ErrKeyNotFound) {
return 0, nil
}

Expand Down
3 changes: 2 additions & 1 deletion dot/types/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package types

import (
"bytes"
"errors"
"fmt"
"math/big"

Expand Down Expand Up @@ -62,7 +63,7 @@ func NewBodyFromExtrinsicStrings(ss []string) (*Body, error) {
exts := []Extrinsic{}
for _, s := range ss {
b, err := common.HexToBytes(s)
if err == common.ErrNoPrefix {
if errors.Is(err, common.ErrNoPrefix) {
b = []byte(s)
} else if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions lib/grandpa/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func (s *Service) initiate() error {
}

err = s.playGrandpaRound()
if err == ErrServicePaused {
if errors.Is(err, ErrServicePaused) {
logger.Info("service paused")
// wait for service to un-pause
<-s.resumed
Expand Down Expand Up @@ -1163,7 +1163,7 @@ func (s *Service) getPossibleSelectedAncestors(votes []Vote, curr common.Hash,

// find common ancestor, check if votes for it is >threshold or not
pred, err := s.blockState.HighestCommonAncestor(v.Hash, curr)
if err == blocktree.ErrNodeNotFound {
if errors.Is(err, blocktree.ErrNodeNotFound) {
continue
} else if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion tests/stress/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package stress

import (
"errors"
"fmt"
"sync"
"testing"
Expand Down Expand Up @@ -194,7 +195,7 @@ func compareFinalizedHeadsWithRetry(t *testing.T, nodes []*utils.Node, round uin
break
}

if err == errFinalizedBlockMismatch {
if errors.Is(err, errFinalizedBlockMismatch) {
return common.Hash{}, fmt.Errorf("%w: round=%d hashes=%v", err, round, hashes)
}

Expand Down

0 comments on commit be078e5

Please sign in to comment.