github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/p2p/peer_error.go (about) 1 package p2p 2 3 import ( 4 "fmt" 5 ) 6 7 const ( 8 errMagicTokenMismatch = iota 9 errRead 10 errWrite 11 errMisc 12 errInvalidMsgCode 13 errInvalidMsg 14 errP2PVersionMismatch 15 errPubkeyInvalid 16 errPubkeyForbidden 17 errProtocolBreach 18 errPingTimeout 19 errInvalidNetworkId 20 errInvalidProtocolVersion 21 ) 22 23 var errorToString = map[int]string{ 24 errMagicTokenMismatch: "magic token mismatch", 25 errRead: "read error", 26 errWrite: "write error", 27 errMisc: "misc error", 28 errInvalidMsgCode: "invalid message code", 29 errInvalidMsg: "invalid message", 30 errP2PVersionMismatch: "P2P Version Mismatch", 31 errPubkeyInvalid: "public key invalid", 32 errPubkeyForbidden: "public key forbidden", 33 errProtocolBreach: "protocol Breach", 34 errPingTimeout: "ping timeout", 35 errInvalidNetworkId: "invalid network id", 36 errInvalidProtocolVersion: "invalid protocol version", 37 } 38 39 type peerError struct { 40 Code int 41 message string 42 } 43 44 func newPeerError(code int, format string, v ...interface{}) *peerError { 45 desc, ok := errorToString[code] 46 if !ok { 47 panic("invalid error code") 48 } 49 err := &peerError{code, desc} 50 if format != "" { 51 err.message += ": " + fmt.Sprintf(format, v...) 52 } 53 return err 54 } 55 56 func (self *peerError) Error() string { 57 return self.message 58 } 59 60 type DiscReason byte 61 62 const ( 63 DiscRequested DiscReason = iota 64 DiscNetworkError 65 DiscProtocolError 66 DiscUselessPeer 67 DiscTooManyPeers 68 DiscAlreadyConnected 69 DiscIncompatibleVersion 70 DiscInvalidIdentity 71 DiscQuitting 72 DiscUnexpectedIdentity 73 DiscSelf 74 DiscReadTimeout 75 DiscSubprotocolError 76 ) 77 78 var discReasonToString = [...]string{ 79 DiscRequested: "Disconnect requested", 80 DiscNetworkError: "Network error", 81 DiscProtocolError: "Breach of protocol", 82 DiscUselessPeer: "Useless peer", 83 DiscTooManyPeers: "Too many peers", 84 DiscAlreadyConnected: "Already connected", 85 DiscIncompatibleVersion: "Incompatible P2P protocol version", 86 DiscInvalidIdentity: "Invalid node identity", 87 DiscQuitting: "Client quitting", 88 DiscUnexpectedIdentity: "Unexpected identity", 89 DiscSelf: "Connected to self", 90 DiscReadTimeout: "Read timeout", 91 DiscSubprotocolError: "Subprotocol error", 92 } 93 94 func (d DiscReason) String() string { 95 if len(discReasonToString) < int(d) { 96 return fmt.Sprintf("Unknown Reason(%d)", d) 97 } 98 return discReasonToString[d] 99 } 100 101 type discRequestedError DiscReason 102 103 func (err discRequestedError) Error() string { 104 return fmt.Sprintf("disconnect requested: %v", DiscReason(err)) 105 } 106 107 func discReasonForError(err error) DiscReason { 108 if reason, ok := err.(discRequestedError); ok { 109 return DiscReason(reason) 110 } 111 peerError, ok := err.(*peerError) 112 if !ok { 113 return DiscSubprotocolError 114 } 115 switch peerError.Code { 116 case errP2PVersionMismatch: 117 return DiscIncompatibleVersion 118 case errPubkeyInvalid: 119 return DiscInvalidIdentity 120 case errPubkeyForbidden: 121 return DiscUselessPeer 122 case errInvalidMsgCode, errMagicTokenMismatch, errProtocolBreach: 123 return DiscProtocolError 124 case errPingTimeout: 125 return DiscReadTimeout 126 case errRead, errWrite: 127 return DiscNetworkError 128 default: 129 return DiscSubprotocolError 130 } 131 }