peer_error.go 2.82 KB
Newer Older
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 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
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 13 14
// GNU Lesser General Public License for more details.
//
// 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

zelig's avatar
zelig committed
17 18 19 20 21 22 23
package p2p

import (
	"fmt"
)

const (
24
	errInvalidMsgCode = iota
25
	errInvalidMsg
zelig's avatar
zelig committed
26 27
)

28
var errorToString = map[int]string{
29 30
	errInvalidMsgCode: "invalid message code",
	errInvalidMsg:     "invalid message",
zelig's avatar
zelig committed
31 32
}

33
type peerError struct {
34
	code    int
zelig's avatar
zelig committed
35 36 37
	message string
}

38
func newPeerError(code int, format string, v ...interface{}) *peerError {
zelig's avatar
zelig committed
39 40 41 42
	desc, ok := errorToString[code]
	if !ok {
		panic("invalid error code")
	}
43 44 45 46 47
	err := &peerError{code, desc}
	if format != "" {
		err.message += ": " + fmt.Sprintf(format, v...)
	}
	return err
zelig's avatar
zelig committed
48 49
}

50
func (self *peerError) Error() string {
zelig's avatar
zelig committed
51 52 53
	return self.message
}

54
type DiscReason uint
55 56

const (
57 58 59 60 61 62 63 64 65 66 67 68
	DiscRequested DiscReason = iota
	DiscNetworkError
	DiscProtocolError
	DiscUselessPeer
	DiscTooManyPeers
	DiscAlreadyConnected
	DiscIncompatibleVersion
	DiscInvalidIdentity
	DiscQuitting
	DiscUnexpectedIdentity
	DiscSelf
	DiscReadTimeout
69
	DiscSubprotocolError = 0x10
70 71
)

72
var discReasonToString = [...]string{
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	DiscRequested:           "Disconnect requested",
	DiscNetworkError:        "Network error",
	DiscProtocolError:       "Breach of protocol",
	DiscUselessPeer:         "Useless peer",
	DiscTooManyPeers:        "Too many peers",
	DiscAlreadyConnected:    "Already connected",
	DiscIncompatibleVersion: "Incompatible P2P protocol version",
	DiscInvalidIdentity:     "Invalid node identity",
	DiscQuitting:            "Client quitting",
	DiscUnexpectedIdentity:  "Unexpected identity",
	DiscSelf:                "Connected to self",
	DiscReadTimeout:         "Read timeout",
	DiscSubprotocolError:    "Subprotocol error",
}

func (d DiscReason) String() string {
	if len(discReasonToString) < int(d) {
		return fmt.Sprintf("Unknown Reason(%d)", d)
	}
	return discReasonToString[d]
}

95 96
func (d DiscReason) Error() string {
	return d.String()
97 98
}

99
func discReasonForError(err error) DiscReason {
100 101
	if reason, ok := err.(DiscReason); ok {
		return reason
102
	}
103
	peerError, ok := err.(*peerError)
104 105 106 107 108 109 110
	if ok {
		switch peerError.code {
		case errInvalidMsgCode, errInvalidMsg:
			return DiscProtocolError
		default:
			return DiscSubprotocolError
		}
111
	}
112
	return DiscSubprotocolError
zelig's avatar
zelig committed
113
}