github.com/aswedchain/aswed@v1.0.1/p2p/peer_error.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package p2p 18 19 import ( 20 "errors" 21 "fmt" 22 ) 23 24 const ( 25 errInvalidMsgCode = iota 26 errInvalidMsg 27 ) 28 29 var errorToString = map[int]string{ 30 errInvalidMsgCode: "invalid message code", 31 errInvalidMsg: "invalid message", 32 } 33 34 type peerError struct { 35 code int 36 message string 37 } 38 39 func newPeerError(code int, format string, v ...interface{}) *peerError { 40 desc, ok := errorToString[code] 41 if !ok { 42 panic("invalid error code") 43 } 44 err := &peerError{code, desc} 45 if format != "" { 46 err.message += ": " + fmt.Sprintf(format, v...) 47 } 48 return err 49 } 50 51 func (pe *peerError) Error() string { 52 return pe.message 53 } 54 55 var errProtocolReturned = errors.New("protocol returned") 56 57 type DiscReason uint 58 59 const ( 60 DiscRequested DiscReason = iota 61 DiscNetworkError 62 DiscProtocolError 63 DiscUselessPeer 64 DiscTooManyPeers 65 DiscAlreadyConnected 66 DiscIncompatibleVersion 67 DiscInvalidIdentity 68 DiscQuitting 69 DiscUnexpectedIdentity 70 DiscSelf 71 DiscReadTimeout 72 DiscSubprotocolError = 0x10 73 ) 74 75 var discReasonToString = [...]string{ 76 DiscRequested: "disconnect requested", 77 DiscNetworkError: "network error", 78 DiscProtocolError: "breach of protocol", 79 DiscUselessPeer: "useless peer", 80 DiscTooManyPeers: "too many peers", 81 DiscAlreadyConnected: "already connected", 82 DiscIncompatibleVersion: "incompatible p2p protocol version", 83 DiscInvalidIdentity: "invalid node identity", 84 DiscQuitting: "client quitting", 85 DiscUnexpectedIdentity: "unexpected identity", 86 DiscSelf: "connected to self", 87 DiscReadTimeout: "read timeout", 88 DiscSubprotocolError: "subprotocol error", 89 } 90 91 func (d DiscReason) String() string { 92 if len(discReasonToString) < int(d) { 93 return fmt.Sprintf("unknown disconnect reason %d", d) 94 } 95 return discReasonToString[d] 96 } 97 98 func (d DiscReason) Error() string { 99 return d.String() 100 } 101 102 func discReasonForError(err error) DiscReason { 103 if reason, ok := err.(DiscReason); ok { 104 return reason 105 } 106 if err == errProtocolReturned { 107 return DiscQuitting 108 } 109 peerError, ok := err.(*peerError) 110 if ok { 111 switch peerError.code { 112 case errInvalidMsgCode, errInvalidMsg: 113 return DiscProtocolError 114 default: 115 return DiscSubprotocolError 116 } 117 } 118 return DiscSubprotocolError 119 }