github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/network/p2p/peer_error.go (about)

     1  package p2p
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  )
     7  
     8  const (
     9  	errInvalidMsgCode = iota
    10  	errInvalidMsg
    11  )
    12  
    13  var errorToString = map[int]string{
    14  	errInvalidMsgCode: "invalid message code",
    15  	errInvalidMsg:     "invalid message",
    16  }
    17  
    18  type peerError struct {
    19  	code    int
    20  	message string
    21  }
    22  
    23  func newPeerError(code int, format string, v ...interface{}) *peerError {
    24  	desc, ok := errorToString[code]
    25  	if !ok {
    26  		panic("invalid error code")
    27  	}
    28  	err := &peerError{code, desc}
    29  	if format != "" {
    30  		err.message += ": " + fmt.Sprintf(format, v...)
    31  	}
    32  	return err
    33  }
    34  
    35  func (self *peerError) Error() string {
    36  	return self.message
    37  }
    38  
    39  var errProtocolReturned = errors.New("protocol returned")
    40  
    41  type DiscReason uint
    42  
    43  const (
    44  	DiscRequested DiscReason = iota
    45  	DiscNetworkError
    46  	DiscProtocolError
    47  	DiscUselessPeer
    48  	DiscTooManyPeers
    49  	DiscAlreadyConnected
    50  	DiscIncompatibleVersion
    51  	DiscInvalidIdentity
    52  	DiscQuitting
    53  	DiscUnexpectedIdentity
    54  	DiscSelf
    55  	DiscReadTimeout
    56  	DiscSubprotocolError = 0x10
    57  )
    58  
    59  var discReasonToString = [...]string{
    60  	DiscRequested:           "disconnect requested",
    61  	DiscNetworkError:        "network error",
    62  	DiscProtocolError:       "breach of protocol",
    63  	DiscUselessPeer:         "useless peer",
    64  	DiscTooManyPeers:        "too many peers",
    65  	DiscAlreadyConnected:    "already connected",
    66  	DiscIncompatibleVersion: "incompatible p2p protocol version",
    67  	DiscInvalidIdentity:     "invalid node identity",
    68  	DiscQuitting:            "client quitting",
    69  	DiscUnexpectedIdentity:  "unexpected identity",
    70  	DiscSelf:                "connected to self",
    71  	DiscReadTimeout:         "read timeout",
    72  	DiscSubprotocolError:    "subprotocol error",
    73  }
    74  
    75  func (d DiscReason) String() string {
    76  	if len(discReasonToString) < int(d) {
    77  		return fmt.Sprintf("unknown disconnect reason %d", d)
    78  	}
    79  	return discReasonToString[d]
    80  }
    81  
    82  func (d DiscReason) Error() string {
    83  	return d.String()
    84  }
    85  
    86  func discReasonForError(err error) DiscReason {
    87  	if reason, ok := err.(DiscReason); ok {
    88  		return reason
    89  	}
    90  	if err == errProtocolReturned {
    91  		return DiscQuitting
    92  	}
    93  	peerError, ok := err.(*peerError)
    94  	if ok {
    95  		switch peerError.code {
    96  		case errInvalidMsgCode, errInvalidMsg:
    97  			return DiscProtocolError
    98  		default:
    99  			return DiscSubprotocolError
   100  		}
   101  	}
   102  	return DiscSubprotocolError
   103  }