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

     1  package network
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/libp2p/go-libp2p/core/peer"
     8  
     9  	p2plogging "github.com/onflow/flow-go/network/p2p/logging"
    10  )
    11  
    12  var (
    13  	EmptyTargetList = errors.New("target list empty")
    14  )
    15  
    16  // ErrIllegalConnectionState indicates connection status to node is NotConnected but connections to node > 0
    17  type ErrIllegalConnectionState struct {
    18  	pid        peer.ID
    19  	numOfConns int
    20  }
    21  
    22  func (e ErrIllegalConnectionState) Error() string {
    23  	return fmt.Sprintf("unexpected connection status to peer %s: received NotConnected status while connection list is not empty %d ", p2plogging.PeerId(e.pid), e.numOfConns)
    24  }
    25  
    26  // NewConnectionStatusErr returns a new ErrIllegalConnectionState.
    27  func NewConnectionStatusErr(pid peer.ID, numOfConns int) ErrIllegalConnectionState {
    28  	return ErrIllegalConnectionState{pid: pid, numOfConns: numOfConns}
    29  }
    30  
    31  // IsErrConnectionStatus returns whether an error is ErrIllegalConnectionState
    32  func IsErrConnectionStatus(err error) bool {
    33  	var e ErrIllegalConnectionState
    34  	return errors.As(err, &e)
    35  }
    36  
    37  // TransientError represents an error returned from a network layer function call
    38  // which may be interpreted as non-critical. In general, we desire that all expected
    39  // error return values are enumerated in a function's documentation - any undocumented
    40  // errors are considered fatal. However, 3rd party libraries don't always conform to
    41  // this standard, including the networking libraries we use. This error type can be
    42  // used to wrap these 3rd party errors on the boundary into flow-go, to explicitly
    43  // mark them as non-critical.
    44  type TransientError struct {
    45  	Err error
    46  }
    47  
    48  func (err TransientError) Error() string {
    49  	return err.Err.Error()
    50  }
    51  
    52  func (err TransientError) Unwrap() error {
    53  	return err.Err
    54  }
    55  
    56  func NewTransientErrorf(msg string, args ...interface{}) TransientError {
    57  	return TransientError{
    58  		Err: fmt.Errorf(msg, args...),
    59  	}
    60  }
    61  
    62  func IsTransientError(err error) bool {
    63  	var errTransient TransientError
    64  	return errors.As(err, &errTransient)
    65  }