Skip to content

Commit

Permalink
refactor: unify the error handling methods in the crypto package that…
Browse files Browse the repository at this point in the history
… are different from the project style

Signed-off-by: ChengenH <[email protected]>
  • Loading branch information
ChengenH committed Dec 13, 2024
1 parent 86b2905 commit 04de66c
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/dispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ var disputerStartCmd = &cmds.Command{

for {
err := disputeLoop()
if err == context.Canceled {
if errors.Is(err, context.Canceled) {
disputeLog.Info("disputer shutting down")
break
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/chainsync/slashfilter/mysqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slashfilter

import (
"context"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -69,7 +70,7 @@ func NewMysqlSlashFilter(cfg config.MySQLConfig) (ISlashFilter, error) {
func (f *MysqlSlashFilter) checkSameHeightFault(bh *types.BlockHeader) (cid.Cid, bool, error) {
var bk MinedBlock
err := f._db.Model(&MinedBlock{}).Take(&bk, "miner=? and epoch=?", bh.Miner.String(), bh.Height).Error
if err == gorm.ErrRecordNotFound {
if errors.Is(err, gorm.ErrRecordNotFound) {
return cid.Undef, false, nil
}

Expand All @@ -90,7 +91,7 @@ func (f *MysqlSlashFilter) checkSameHeightFault(bh *types.BlockHeader) (cid.Cid,
func (f *MysqlSlashFilter) checkSameParentFault(bh *types.BlockHeader) (cid.Cid, bool, error) {
var bk MinedBlock
err := f._db.Model(&MinedBlock{}).Take(&bk, "miner=? and parent_key=?", bh.Miner.String(), types.NewTipSetKey(bh.Parents...).String()).Error
if err == gorm.ErrRecordNotFound {
if errors.Is(err, gorm.ErrRecordNotFound) {
return cid.Undef, false, nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/ethhashlookup/eth_transaction_hash_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (ei *EthTxHashLookup) GetCidFromHash(txHash types.EthHash) (cid.Cid, error)
var c string
err := row.Scan(&c)
if err != nil {
if err == sql.ErrNoRows {
if errors.Is(err, sql.ErrNoRows) {
return cid.Undef, ErrNotFound
}
return cid.Undef, err
Expand All @@ -86,7 +86,7 @@ func (ei *EthTxHashLookup) GetHashFromCid(c cid.Cid) (types.EthHash, error) {
var hashString string
err := row.Scan(&c)
if err != nil {
if err == sql.ErrNoRows {
if errors.Is(err, sql.ErrNoRows) {
return types.EmptyEthHash, ErrNotFound
}
return types.EmptyEthHash, err
Expand Down Expand Up @@ -117,7 +117,7 @@ func NewTransactionHashLookup(path string) (*EthTxHashLookup, error) {
}

q, err := db.Query("SELECT name FROM sqlite_master WHERE type='table' AND name='_meta';")
if err == sql.ErrNoRows || !q.Next() {
if errors.Is(err, sql.ErrNoRows) || !q.Next() {
// empty database, create the schema
for _, ddl := range ddls {
if _, err := db.Exec(ddl); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/httpreader/resumable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package httpreader

import (
"context"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -103,7 +104,7 @@ func (r *ResumableReader) Read(p []byte) (n int, err error) {
n, err = r.reader.Read(p)
r.position += int64(n)

if err == io.EOF || err == io.ErrUnexpectedEOF {
if err == io.EOF || errors.Is(err, io.ErrUnexpectedEOF) {
if r.position == r.contentLength {
if err := r.reader.Close(); err != nil {
log.Warnf("error closing reader: %+v", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/paychmgr/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (pm *Manager) AvailableFundsByFromTo(ctx context.Context, from address.Addr
}

ci, err := ca.outboundActiveByFromTo(ctx, from, to)
if err == ErrChannelNotTracked {
if errors.Is(err, ErrChannelNotTracked) {
// If there is no active channel between from / to we still want to
// return an empty ChannelAvailableFunds, so that clients can check
// for the existence of a channel between from / to without getting
Expand Down Expand Up @@ -167,7 +167,7 @@ func (pm *Manager) GetPaychWaitReady(ctx context.Context, mcid cid.Cid) (address
pm.lk.Unlock()

if err != nil {
if err == datastore.ErrNotFound {
if errors.Is(err, datastore.ErrNotFound) {
return address.Undef, fmt.Errorf("could not find wait msg cid %s", mcid)
}
return address.Undef, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/paychmgr/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (ps *Store) ByChannelID(ctx context.Context, channelID string) (*pchTypes.C

res, err := ps.ds.Get(ctx, dskeyForChannel(channelID))
if err != nil {
if err == datastore.ErrNotFound {
if errors.Is(err, datastore.ErrNotFound) {
return nil, ErrChannelNotTracked
}
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion pkg/state/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package state

import (
"context"
"errors"

Check failure on line 5 in pkg/state/view.go

View workflow job for this annotation

GitHub Actions / test

other declaration of errors

Check failure on line 5 in pkg/state/view.go

View workflow job for this annotation

GitHub Actions / check

other declaration of errors
"fmt"
"strconv"

Expand Down Expand Up @@ -277,7 +278,7 @@ func (v *View) MinerExists(ctx context.Context, maddr addr.Address) (bool, error
if err == nil {
return true, nil
}
if err == types.ErrActorNotFound {
if errors.Is(err, types.ErrActorNotFound) {
return false, nil
}
return false, err
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/dag/oldpath/oldresolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (cid.
}

if err != nil {
if err == dag.ErrLinkNotFound {
if errors.Is(err, dag.ErrLinkNotFound) {
err = ErrNoLink{Name: p[0], Node: nd.Cid()}
}
return cid.Cid{}, nil, err
Expand All @@ -104,7 +104,7 @@ func (r *Resolver) ResolveToLastNode(ctx context.Context, fpath path.Path) (cid.
// Confirm the path exists within the object
val, rest, err := nd.Resolve(p)
if err != nil {
if err == dag.ErrLinkNotFound {
if errors.Is(err, dag.ErrLinkNotFound) {
err = ErrNoLink{Name: p[0], Node: nd.Cid()}
}
return cid.Cid{}, nil, err
Expand Down Expand Up @@ -179,7 +179,7 @@ func (r *Resolver) ResolveLinks(ctx context.Context, ndd ipld.Node, names []stri
defer cancel()

lnk, rest, err := r.ResolveOnce(ctx, r.DAG, nd, names)
if err == dag.ErrLinkNotFound {
if errors.Is(err, dag.ErrLinkNotFound) {
return result, ErrNoLink{Name: names[0], Node: nd.Cid()}
} else if err != nil {
return result, err
Expand Down

0 comments on commit 04de66c

Please sign in to comment.