github.com/klaytn/klaytn@v1.10.2/networks/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  // TODO-klaytn: update the type to uint8
    58  type DiscReason uint
    59  
    60  const (
    61  	DiscRequested DiscReason = iota
    62  	DiscNetworkError
    63  	DiscProtocolError
    64  	DiscUselessPeer
    65  	DiscTooManyPeers
    66  	DiscAlreadyConnected
    67  	DiscIncompatibleVersion
    68  	DiscInvalidIdentity
    69  	DiscQuitting
    70  	DiscUnexpectedIdentity
    71  	DiscSelf
    72  	DiscReadTimeout
    73  	DiscSubprotocolError = 0x10
    74  )
    75  
    76  var discReasonToString = [...]string{
    77  	DiscRequested:           "disconnect requested",
    78  	DiscNetworkError:        "network error",
    79  	DiscProtocolError:       "breach of protocol",
    80  	DiscUselessPeer:         "useless peer",
    81  	DiscTooManyPeers:        "too many peers",
    82  	DiscAlreadyConnected:    "already connected",
    83  	DiscIncompatibleVersion: "incompatible p2p protocol version",
    84  	DiscInvalidIdentity:     "invalid node identity",
    85  	DiscQuitting:            "client quitting",
    86  	DiscUnexpectedIdentity:  "unexpected identity",
    87  	DiscSelf:                "connected to self",
    88  	DiscReadTimeout:         "read timeout",
    89  	DiscSubprotocolError:    "subprotocol error",
    90  }
    91  
    92  func (d DiscReason) String() string {
    93  	if uint(len(discReasonToString)) < uint(d) {
    94  		return fmt.Sprintf("unknown disconnect reason %d", d)
    95  	}
    96  	return discReasonToString[d]
    97  }
    98  
    99  func (d DiscReason) Error() string {
   100  	return d.String()
   101  }
   102  
   103  func discReasonForError(err error) DiscReason {
   104  	if reason, ok := err.(DiscReason); ok {
   105  		return reason
   106  	}
   107  	if err == errProtocolReturned {
   108  		return DiscQuitting
   109  	}
   110  	peerError, ok := err.(*peerError)
   111  	if ok {
   112  		switch peerError.code {
   113  		case errInvalidMsgCode, errInvalidMsg:
   114  			return DiscProtocolError
   115  		default:
   116  			return DiscSubprotocolError
   117  		}
   118  	}
   119  	return DiscSubprotocolError
   120  }