github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/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  	"fmt"
    21  )
    22  
    23  const (
    24  	errInvalidMsgCode = iota
    25  	errInvalidMsg
    26  )
    27  
    28  var errorToString = map[int]string{
    29  	errInvalidMsgCode: "invalid message code",
    30  	errInvalidMsg:     "invalid message",
    31  }
    32  
    33  type peerError struct {
    34  	code    int
    35  	message string
    36  }
    37  
    38  func newPeerError(code int, format string, v ...interface{}) *peerError {
    39  	desc, ok := errorToString[code]
    40  	if !ok {
    41  		panic("invalid error code")
    42  	}
    43  	err := &peerError{code, desc}
    44  	if format != "" {
    45  		err.message += ": " + fmt.Sprintf(format, v...)
    46  	}
    47  	return err
    48  }
    49  
    50  func (self *peerError) Error() string {
    51  	return self.message
    52  }
    53  
    54  type DiscReason uint
    55  
    56  const (
    57  	DiscRequested DiscReason = iota
    58  	DiscNetworkError
    59  	DiscProtocolError
    60  	DiscUselessPeer
    61  	DiscTooManyPeers
    62  	DiscAlreadyConnected
    63  	DiscIncompatibleVersion
    64  	DiscInvalidIdentity
    65  	DiscQuitting
    66  	DiscUnexpectedIdentity
    67  	DiscSelf
    68  	DiscReadTimeout
    69  	DiscSubprotocolError = 0x10
    70  )
    71  
    72  var discReasonToString = [...]string{
    73  	DiscRequested:           "Disconnect requested",
    74  	DiscNetworkError:        "Network error",
    75  	DiscProtocolError:       "Breach of protocol",
    76  	DiscUselessPeer:         "Useless peer",
    77  	DiscTooManyPeers:        "Too many peers",
    78  	DiscAlreadyConnected:    "Already connected",
    79  	DiscIncompatibleVersion: "Incompatible P2P protocol version",
    80  	DiscInvalidIdentity:     "Invalid node identity",
    81  	DiscQuitting:            "Client quitting",
    82  	DiscUnexpectedIdentity:  "Unexpected identity",
    83  	DiscSelf:                "Connected to self",
    84  	DiscReadTimeout:         "Read timeout",
    85  	DiscSubprotocolError:    "Subprotocol error",
    86  }
    87  
    88  func (d DiscReason) String() string {
    89  	if len(discReasonToString) < int(d) {
    90  		return fmt.Sprintf("Unknown Reason(%d)", d)
    91  	}
    92  	return discReasonToString[d]
    93  }
    94  
    95  func (d DiscReason) Error() string {
    96  	return d.String()
    97  }
    98  
    99  func discReasonForError(err error) DiscReason {
   100  	if reason, ok := err.(DiscReason); ok {
   101  		return reason
   102  	}
   103  	peerError, ok := err.(*peerError)
   104  	if ok {
   105  		switch peerError.code {
   106  		case errInvalidMsgCode, errInvalidMsg:
   107  			return DiscProtocolError
   108  		default:
   109  			return DiscSubprotocolError
   110  		}
   111  	}
   112  	return DiscSubprotocolError
   113  }