github.com/core-coin/go-core/v2@v2.1.9/p2p/peer_error.go (about)

     1  // Copyright 2014 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core 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 uint8
    58  
    59  const (
    60  	DiscRequested DiscReason = iota
    61  	DiscNetworkError
    62  	DiscProtocolError
    63  	DiscUselessPeer
    64  	DiscTooManyPeers
    65  	DiscTooManyInboundPeers
    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  	DiscTooManyInboundPeers: "too many inbound 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  }