Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use errors.New to replace fmt.Errorf with no parameters #3099

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func ListenAddrs(addrs ...ma.Multiaddr) Option {
func Security(name string, constructor interface{}) Option {
return func(cfg *Config) error {
if cfg.Insecure {
return fmt.Errorf("cannot use security transports with an insecure libp2p configuration")
return errors.New("cannot use security transports with an insecure libp2p configuration")
}
cfg.SecurityTransports = append(cfg.SecurityTransports, config.Security{ID: protocol.ID(name), Constructor: constructor})
return nil
Expand All @@ -84,7 +84,7 @@ func Security(name string, constructor interface{}) Option {
// It's incompatible with all other transport security protocols.
var NoSecurity Option = func(cfg *Config) error {
if len(cfg.SecurityTransports) > 0 {
return fmt.Errorf("cannot use security transports with an insecure libp2p configuration")
return errors.New("cannot use security transports with an insecure libp2p configuration")
}
cfg.Insecure = true
return nil
Expand Down Expand Up @@ -198,7 +198,7 @@ func Transport(constructor interface{}, opts ...interface{}) Option {
func Peerstore(ps peerstore.Peerstore) Option {
return func(cfg *Config) error {
if cfg.Peerstore != nil {
return fmt.Errorf("cannot specify multiple peerstore options")
return errors.New("cannot specify multiple peerstore options")
}

cfg.Peerstore = ps
Expand All @@ -210,7 +210,7 @@ func Peerstore(ps peerstore.Peerstore) Option {
func PrivateNetwork(psk pnet.PSK) Option {
return func(cfg *Config) error {
if cfg.PSK != nil {
return fmt.Errorf("cannot specify multiple private network options")
return errors.New("cannot specify multiple private network options")
}

cfg.PSK = psk
Expand All @@ -222,7 +222,7 @@ func PrivateNetwork(psk pnet.PSK) Option {
func BandwidthReporter(rep metrics.Reporter) Option {
return func(cfg *Config) error {
if cfg.Reporter != nil {
return fmt.Errorf("cannot specify multiple bandwidth reporter options")
return errors.New("cannot specify multiple bandwidth reporter options")
}

cfg.Reporter = rep
Expand All @@ -234,7 +234,7 @@ func BandwidthReporter(rep metrics.Reporter) Option {
func Identity(sk crypto.PrivKey) Option {
return func(cfg *Config) error {
if cfg.PeerKey != nil {
return fmt.Errorf("cannot specify multiple identities")
return errors.New("cannot specify multiple identities")
}

cfg.PeerKey = sk
Expand All @@ -249,7 +249,7 @@ func Identity(sk crypto.PrivKey) Option {
func ConnectionManager(connman connmgr.ConnManager) Option {
return func(cfg *Config) error {
if cfg.ConnManager != nil {
return fmt.Errorf("cannot specify multiple connection managers")
return errors.New("cannot specify multiple connection managers")
}
cfg.ConnManager = connman
return nil
Expand All @@ -260,7 +260,7 @@ func ConnectionManager(connman connmgr.ConnManager) Option {
func AddrsFactory(factory config.AddrsFactory) Option {
return func(cfg *Config) error {
if cfg.AddrsFactory != nil {
return fmt.Errorf("cannot specify multiple address factories")
return errors.New("cannot specify multiple address factories")
}
cfg.AddrsFactory = factory
return nil
Expand Down Expand Up @@ -426,7 +426,7 @@ func NATPortMap() Option {
func NATManager(nm config.NATManagerC) Option {
return func(cfg *Config) error {
if cfg.NATManager != nil {
return fmt.Errorf("cannot specify multiple NATManagers")
return errors.New("cannot specify multiple NATManagers")
}
cfg.NATManager = nm
return nil
Expand All @@ -445,7 +445,7 @@ func Ping(enable bool) Option {
func Routing(rt config.RoutingC) Option {
return func(cfg *Config) error {
if cfg.Routing != nil {
return fmt.Errorf("cannot specify multiple routing options")
return errors.New("cannot specify multiple routing options")
}
cfg.Routing = rt
return nil
Expand Down
8 changes: 4 additions & 4 deletions p2p/host/eventbus/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type emitter struct {

func (e *emitter) Emit(evt interface{}) error {
if e.closed.Load() {
return fmt.Errorf("emitter is closed")
return errors.New("emitter is closed")
}

e.n.emit(evt)
Expand All @@ -58,7 +58,7 @@ func (e *emitter) Emit(evt interface{}) error {

func (e *emitter) Close() error {
if !e.closed.CompareAndSwap(false, true) {
return fmt.Errorf("closed an emitter more than once")
return errors.New("closed an emitter more than once")
}
if e.n.nEmitters.Add(-1) == 0 {
e.dropper(e.typ)
Expand Down Expand Up @@ -238,7 +238,7 @@ func (b *basicBus) Subscribe(evtTypes interface{}, opts ...event.SubscriptionOpt
if len(types) > 1 {
for _, t := range types {
if t == event.WildcardSubscription {
return nil, fmt.Errorf("wildcard subscriptions must be started separately")
return nil, errors.New("wildcard subscriptions must be started separately")
}
}
}
Expand Down Expand Up @@ -293,7 +293,7 @@ func (b *basicBus) Subscribe(evtTypes interface{}, opts ...event.SubscriptionOpt
// emit(EventT{})
func (b *basicBus) Emitter(evtType interface{}, opts ...event.EmitterOpt) (e event.Emitter, err error) {
if evtType == event.WildcardSubscription {
return nil, fmt.Errorf("illegal emitter for wildcard subscription")
return nil, errors.New("illegal emitter for wildcard subscription")
}

var settings emitterSettings
Expand Down
5 changes: 3 additions & 2 deletions p2p/host/peerstore/pstoreds/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pstoreds
import (
"bytes"
"context"
"errors"
"fmt"
"sort"
"sync"
Expand Down Expand Up @@ -290,10 +291,10 @@ func (ab *dsAddrBook) ConsumePeerRecord(recordEnvelope *record.Envelope, ttl tim
}
rec, ok := r.(*peer.PeerRecord)
if !ok {
return false, fmt.Errorf("envelope did not contain PeerRecord")
return false, errors.New("envelope did not contain PeerRecord")
}
if !rec.PeerID.MatchesPublicKey(recordEnvelope.PublicKey) {
return false, fmt.Errorf("signing key does not match PeerID in PeerRecord")
return false, errors.New("signing key does not match PeerID in PeerRecord")
}

// ensure that the seq number from envelope is >= any previously received seq no
Expand Down
3 changes: 1 addition & 2 deletions p2p/host/peerstore/pstoreds/protobook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pstoreds

import (
"errors"
"fmt"
"sync"

"github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -184,7 +183,7 @@ func (pb *dsProtoBook) getProtocolMap(p peer.ID) (map[protocol.ID]struct{}, erro
case nil:
cast, ok := iprotomap.(map[protocol.ID]struct{})
if !ok {
return nil, fmt.Errorf("stored protocol set was not a map")
return nil, errors.New("stored protocol set was not a map")
}

return cast, nil
Expand Down
5 changes: 2 additions & 3 deletions p2p/host/peerstore/pstoremem/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"container/heap"
"context"
"errors"
"fmt"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -315,10 +314,10 @@ func (mab *memoryAddrBook) ConsumePeerRecord(recordEnvelope *record.Envelope, tt
}
rec, ok := r.(*peer.PeerRecord)
if !ok {
return false, fmt.Errorf("unable to process envelope: not a PeerRecord")
return false, errors.New("unable to process envelope: not a PeerRecord")
}
if !rec.PeerID.MatchesPublicKey(recordEnvelope.PublicKey) {
return false, fmt.Errorf("signing key does not match PeerID in PeerRecord")
return false, errors.New("signing key does not match PeerID in PeerRecord")
}

mab.mu.Lock()
Expand Down
5 changes: 3 additions & 2 deletions p2p/host/resource-manager/rcmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rcmgr

import (
"context"
"errors"
"fmt"
"net/netip"
"strings"
Expand Down Expand Up @@ -352,7 +353,7 @@ func (r *resourceManager) OpenConnection(dir network.Direction, usefd bool, endp

ipAddr, ok := netip.AddrFromSlice(ip)
if !ok {
return nil, fmt.Errorf("failed to convert ip to netip.Addr")
return nil, errors.New("failed to convert ip to netip.Addr")
}
return r.openConnection(dir, usefd, endpoint, ipAddr)
}
Expand Down Expand Up @@ -757,7 +758,7 @@ func (s *connectionScope) SetPeer(p peer.ID) error {
defer s.Unlock()

if s.peer != nil {
return fmt.Errorf("connection scope already attached to a peer")
return errors.New("connection scope already attached to a peer")
}

system := s.rcmgr.system
Expand Down
4 changes: 2 additions & 2 deletions p2p/host/routed/routed.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package routedhost

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

"github.com/libp2p/go-libp2p/core/connmgr"
Expand Down Expand Up @@ -151,7 +151,7 @@ func (rh *RoutedHost) findPeerAddrs(ctx context.Context, id peer.ID) ([]ma.Multi
}

if pi.ID != id {
err = fmt.Errorf("routing failure: provided addrs for different peer")
err = errors.New("routing failure: provided addrs for different peer")
log.Errorw("got wrong peer",
"error", err,
"wantedPeer", id,
Expand Down
6 changes: 3 additions & 3 deletions p2p/http/auth/internal/handshake/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ type sigParam struct {

func verifySig(publicKey crypto.PubKey, prefix string, signedParts []sigParam, sig []byte) error {
if publicKey == nil {
return fmt.Errorf("no public key to verify signature")
return errors.New("no public key to verify signature")
}

b := pool.Get(4096)
Expand All @@ -183,15 +183,15 @@ func verifySig(publicKey crypto.PubKey, prefix string, signedParts []sigParam, s
return err
}
if !ok {
return fmt.Errorf("signature verification failed")
return errors.New("signature verification failed")
}

return nil
}

func sign(privKey crypto.PrivKey, prefix string, partsToSign []sigParam) ([]byte, error) {
if privKey == nil {
return nil, fmt.Errorf("no private key available to sign")
return nil, errors.New("no private key available to sign")
}
b := pool.Get(4096)
defer pool.Put(b)
Expand Down
13 changes: 7 additions & 6 deletions p2p/net/mock/mock_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mocknet
import (
"context"
"crypto/rand"
"errors"
"fmt"
"net"
"sort"
Expand Down Expand Up @@ -277,11 +278,11 @@ func (mn *mocknet) LinkPeers(p1, p2 peer.ID) (Link, error) {
mn.Unlock()

if n1 == nil {
return nil, fmt.Errorf("network for p1 not in mocknet")
return nil, errors.New("network for p1 not in mocknet")
}

if n2 == nil {
return nil, fmt.Errorf("network for p2 not in mocknet")
return nil, errors.New("network for p2 not in mocknet")
}

return mn.LinkNets(n1, n2)
Expand All @@ -292,11 +293,11 @@ func (mn *mocknet) validate(n network.Network) (*peernet, error) {

nr, ok := n.(*peernet)
if !ok {
return nil, fmt.Errorf("network not supported (use mock package nets only)")
return nil, errors.New("network not supported (use mock package nets only)")
}

if _, found := mn.nets[nr.peer]; !found {
return nil, fmt.Errorf("network not on mocknet. is it from another mocknet?")
return nil, errors.New("network not on mocknet. is it from another mocknet?")
}

return nr, nil
Expand Down Expand Up @@ -326,7 +327,7 @@ func (mn *mocknet) Unlink(l2 Link) error {

l, ok := l2.(*link)
if !ok {
return fmt.Errorf("only links from mocknet are supported")
return errors.New("only links from mocknet are supported")
}

mn.removeLink(l)
Expand All @@ -336,7 +337,7 @@ func (mn *mocknet) Unlink(l2 Link) error {
func (mn *mocknet) UnlinkPeers(p1, p2 peer.ID) error {
ls := mn.LinksBetweenPeers(p1, p2)
if ls == nil {
return fmt.Errorf("no link between p1 and p2")
return errors.New("no link between p1 and p2")
}

for _, l := range ls {
Expand Down
2 changes: 1 addition & 1 deletion p2p/net/swarm/swarm_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (s *Swarm) dialPeer(ctx context.Context, p peer.ID) (*Conn, error) {
if conn.RemotePeer() != p {
conn.Close()
log.Errorw("Handshake failed to properly authenticate peer", "authenticated", conn.RemotePeer(), "expected", p)
return nil, fmt.Errorf("unexpected peer")
return nil, errors.New("unexpected peer")
}
return conn, nil
}
Expand Down
3 changes: 2 additions & 1 deletion p2p/net/upgrader/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package upgrader

import (
"context"
"errors"
"fmt"
"strings"
"sync"
Expand Down Expand Up @@ -63,7 +64,7 @@ func (l *listener) handleIncoming() {
// make sure we're closed
l.Listener.Close()
if l.err == nil {
l.err = fmt.Errorf("listener closed")
l.err = errors.New("listener closed")
}

wg.Wait()
Expand Down
2 changes: 1 addition & 1 deletion p2p/net/upgrader/upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (u *upgrader) negotiateMuxer(nc net.Conn, isServer bool) (*StreamMuxer, err
if m := u.getMuxerByID(proto); m != nil {
return m, nil
}
return nil, fmt.Errorf("selected protocol we don't have a transport for")
return nil, errors.New("selected protocol we don't have a transport for")
}

func (u *upgrader) getMuxerByID(id protocol.ID) *StreamMuxer {
Expand Down
4 changes: 2 additions & 2 deletions p2p/test/resource-manager/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package itest

import (
"context"
"fmt"
"errors"
"io"
"sync"
"time"
Expand Down Expand Up @@ -286,7 +286,7 @@ func (e *Echo) Echo(p peer.ID, what string) error {
}

if what != string(buf[:n]) {
return fmt.Errorf("echo output doesn't match input")
return errors.New("echo output doesn't match input")
}

return nil
Expand Down
Loading