github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/unicast/stream/errors.go (about)

     1  package stream
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/libp2p/go-libp2p/core/peer"
     8  	"github.com/libp2p/go-libp2p/core/protocol"
     9  
    10  	p2plogging "github.com/onflow/flow-go/network/p2p/logging"
    11  )
    12  
    13  // ErrSecurityProtocolNegotiationFailed indicates security protocol negotiation failed during the stream factory connect attempt.
    14  type ErrSecurityProtocolNegotiationFailed struct {
    15  	pid peer.ID
    16  	err error
    17  }
    18  
    19  func (e ErrSecurityProtocolNegotiationFailed) Error() string {
    20  	return fmt.Errorf("failed to dial remote peer %s in stream factory invalid node ID: %w", p2plogging.PeerId(e.pid), e.err).Error()
    21  }
    22  
    23  // IsErrSecurityProtocolNegotiationFailed returns whether an error is ErrSecurityProtocolNegotiationFailed.
    24  func IsErrSecurityProtocolNegotiationFailed(err error) bool {
    25  	var e ErrSecurityProtocolNegotiationFailed
    26  	return errors.As(err, &e)
    27  }
    28  
    29  // NewSecurityProtocolNegotiationErr returns a new ErrSecurityProtocolNegotiationFailed.
    30  func NewSecurityProtocolNegotiationErr(pid peer.ID, err error) ErrSecurityProtocolNegotiationFailed {
    31  	return ErrSecurityProtocolNegotiationFailed{pid: pid, err: err}
    32  }
    33  
    34  // ErrProtocolNotSupported indicates node is running on a different spork.
    35  type ErrProtocolNotSupported struct {
    36  	peerID     peer.ID
    37  	protocolID protocol.ID
    38  	err        error
    39  }
    40  
    41  func (e ErrProtocolNotSupported) Error() string {
    42  	return fmt.Errorf("failed to dial remote peer %s remote node is running on a different spork: %w, protocol attempted: %s",
    43  		p2plogging.PeerId(e.peerID),
    44  		e.err,
    45  		e.protocolID).Error()
    46  }
    47  
    48  // NewProtocolNotSupportedErr returns a new ErrSecurityProtocolNegotiationFailed.
    49  func NewProtocolNotSupportedErr(peerID peer.ID, protocolID protocol.ID, err error) ErrProtocolNotSupported {
    50  	return ErrProtocolNotSupported{peerID: peerID, protocolID: protocolID, err: err}
    51  }
    52  
    53  // IsErrProtocolNotSupported returns whether an error is ErrProtocolNotSupported.
    54  func IsErrProtocolNotSupported(err error) bool {
    55  	var e ErrProtocolNotSupported
    56  	return errors.As(err, &e)
    57  }
    58  
    59  // ErrGaterDisallowedConnection wrapper around github.com/libp2p/go-libp2p/p2p/net/swarm.ErrGaterDisallowedConnection.
    60  type ErrGaterDisallowedConnection struct {
    61  	err error
    62  }
    63  
    64  func (e ErrGaterDisallowedConnection) Error() string {
    65  	return fmt.Errorf("target node is not on the approved list of nodes: %w", e.err).Error()
    66  }
    67  
    68  // NewGaterDisallowedConnectionErr returns a new ErrGaterDisallowedConnection.
    69  func NewGaterDisallowedConnectionErr(err error) ErrGaterDisallowedConnection {
    70  	return ErrGaterDisallowedConnection{err: err}
    71  }
    72  
    73  // IsErrGaterDisallowedConnection returns whether an error is ErrGaterDisallowedConnection.
    74  func IsErrGaterDisallowedConnection(err error) bool {
    75  	var e ErrGaterDisallowedConnection
    76  	return errors.As(err, &e)
    77  }