github.com/Unheilbar/quorum@v1.0.0/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  // Quorum
    30  //
    31  // Constants for peer connection errors
    32  const (
    33  	// When permissioning is enabled, and node is not permissioned in the network
    34  	errPermissionDenied = iota + 100
    35  	// Unauthorized node joining existing raft cluster
    36  	errNotInRaftCluster
    37  )
    38  
    39  var errorToString = map[int]string{
    40  	errInvalidMsgCode: "invalid message code",
    41  	errInvalidMsg:     "invalid message",
    42  	// Quorum
    43  	errPermissionDenied: "permission denied",
    44  	errNotInRaftCluster: "not in raft cluster",
    45  }
    46  
    47  type peerError struct {
    48  	code    int
    49  	message string
    50  }
    51  
    52  func newPeerError(code int, format string, v ...interface{}) *peerError {
    53  	desc, ok := errorToString[code]
    54  	if !ok {
    55  		panic("invalid error code")
    56  	}
    57  	err := &peerError{code, desc}
    58  	if format != "" {
    59  		err.message += ": " + fmt.Sprintf(format, v...)
    60  	}
    61  	return err
    62  }
    63  
    64  func (pe *peerError) Error() string {
    65  	return pe.message
    66  }
    67  
    68  var errProtocolReturned = errors.New("protocol returned")
    69  
    70  type DiscReason uint
    71  
    72  const (
    73  	DiscRequested DiscReason = iota
    74  	DiscNetworkError
    75  	DiscProtocolError
    76  	DiscUselessPeer
    77  	DiscTooManyPeers
    78  	DiscAlreadyConnected
    79  	DiscIncompatibleVersion
    80  	DiscInvalidIdentity
    81  	DiscQuitting
    82  	DiscUnexpectedIdentity
    83  	DiscSelf
    84  	DiscReadTimeout
    85  	DiscSubprotocolError            = 0x10
    86  	DiscAuthError        DiscReason = 0x20
    87  )
    88  
    89  var discReasonToString = [...]string{
    90  	DiscRequested:           "disconnect requested",
    91  	DiscNetworkError:        "network error",
    92  	DiscProtocolError:       "breach of protocol",
    93  	DiscUselessPeer:         "useless peer",
    94  	DiscTooManyPeers:        "too many peers",
    95  	DiscAlreadyConnected:    "already connected",
    96  	DiscIncompatibleVersion: "incompatible p2p protocol version",
    97  	DiscInvalidIdentity:     "invalid node identity",
    98  	DiscQuitting:            "client quitting",
    99  	DiscUnexpectedIdentity:  "unexpected identity",
   100  	DiscSelf:                "connected to self",
   101  	DiscReadTimeout:         "read timeout",
   102  	DiscSubprotocolError:    "subprotocol error",
   103  	DiscAuthError:           "invalid auth error",
   104  }
   105  
   106  func (d DiscReason) String() string {
   107  	if len(discReasonToString) < int(d) {
   108  		return fmt.Sprintf("unknown disconnect reason %d", d)
   109  	}
   110  	return discReasonToString[d]
   111  }
   112  
   113  func (d DiscReason) Error() string {
   114  	return d.String()
   115  }
   116  
   117  func discReasonForError(err error) DiscReason {
   118  	if reason, ok := err.(DiscReason); ok {
   119  		return reason
   120  	}
   121  	if err == errProtocolReturned {
   122  		return DiscQuitting
   123  	}
   124  	peerError, ok := err.(*peerError)
   125  	if ok {
   126  		switch peerError.code {
   127  		case errInvalidMsgCode, errInvalidMsg:
   128  			return DiscProtocolError
   129  		default:
   130  			return DiscSubprotocolError
   131  		}
   132  	}
   133  	return DiscSubprotocolError
   134  }