github.com/annchain/OG@v0.0.9/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  //go:generate msgp
    25  
    26  const (
    27  	errInvalidMsgCode uint16 = iota
    28  	errInvalidMsg
    29  )
    30  
    31  var errorToString = map[uint16]string{
    32  	errInvalidMsgCode: "invalid message code",
    33  	errInvalidMsg:     "invalid message",
    34  }
    35  
    36  type peerError struct {
    37  	code    uint16
    38  	message string
    39  }
    40  
    41  func newPeerError(code uint16, format string, v ...interface{}) *peerError {
    42  	desc, ok := errorToString[code]
    43  	if !ok {
    44  		panic("invalid error code")
    45  	}
    46  	err := &peerError{code, desc}
    47  	if format != "" {
    48  		err.message += ": " + fmt.Sprintf(format, v...)
    49  	}
    50  	return err
    51  }
    52  
    53  func (pe *peerError) Error() string {
    54  	return pe.message
    55  }
    56  
    57  var errProtocolReturned = errors.New("protocol returned")
    58  
    59  type DiscReason uint16
    60  
    61  const (
    62  	DiscRequested DiscReason = iota
    63  	DiscNetworkError
    64  	DiscProtocolError
    65  	DiscUselessPeer
    66  	DiscTooManyPeers
    67  	DiscAlreadyConnected
    68  	DiscIncompatibleVersion
    69  	DiscInvalidIdentity
    70  	DiscQuitting
    71  	DiscUnexpectedIdentity
    72  	DiscSelf
    73  	DiscReadTimeout
    74  	DiscSubprotocolError = 0x10
    75  )
    76  
    77  var discReasonToString = [...]string{
    78  	DiscRequested:           "disconnect requested",
    79  	DiscNetworkError:        "network error",
    80  	DiscProtocolError:       "breach of protocol",
    81  	DiscUselessPeer:         "useless peer",
    82  	DiscTooManyPeers:        "too many peers",
    83  	DiscAlreadyConnected:    "already connected",
    84  	DiscIncompatibleVersion: "incompatible p2p protocol version",
    85  	DiscInvalidIdentity:     "invalid node identity",
    86  	DiscQuitting:            "client quitting",
    87  	DiscUnexpectedIdentity:  "unexpected identity",
    88  	DiscSelf:                "connected to self",
    89  	DiscReadTimeout:         "read timeout",
    90  	DiscSubprotocolError:    "subprotocol error",
    91  }
    92  
    93  func (d DiscReason) String() string {
    94  	if len(discReasonToString) < int(d) {
    95  		return fmt.Sprintf("unknown disconnect reason %d", d)
    96  	}
    97  	return discReasonToString[d]
    98  }
    99  
   100  func (d DiscReason) Error() string {
   101  	return d.String()
   102  }
   103  
   104  func discReasonForError(err error) DiscReason {
   105  	if reason, ok := err.(DiscReason); ok {
   106  		return reason
   107  	}
   108  	if err == errProtocolReturned {
   109  		return DiscQuitting
   110  	}
   111  	peerError, ok := err.(*peerError)
   112  	if ok {
   113  		switch peerError.code {
   114  		case errInvalidMsgCode, errInvalidMsg:
   115  			return DiscProtocolError
   116  		default:
   117  			return DiscSubprotocolError
   118  		}
   119  	}
   120  	return DiscSubprotocolError
   121  }