aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/peer_error.go
blob: de921878a0e9775441592c6856c877e45932f4e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package p2p

import (
    "fmt"
)

type ErrorCode int

const errorChanCapacity = 10

const (
    PacketTooShort = iota
    PayloadTooShort
    MagicTokenMismatch
    EmptyPayload
    ReadError
    WriteError
    MiscError
    InvalidMsgCode
    InvalidMsg
    P2PVersionMismatch
    PubkeyMissing
    PubkeyInvalid
    PubkeyForbidden
    ProtocolBreach
    PortMismatch
    PingTimeout
    InvalidGenesis
    InvalidNetworkId
    InvalidProtocolVersion
)

var errorToString = map[ErrorCode]string{
    PacketTooShort:         "Packet too short",
    PayloadTooShort:        "Payload too short",
    MagicTokenMismatch:     "Magic token mismatch",
    EmptyPayload:           "Empty payload",
    ReadError:              "Read error",
    WriteError:             "Write error",
    MiscError:              "Misc error",
    InvalidMsgCode:         "Invalid message code",
    InvalidMsg:             "Invalid message",
    P2PVersionMismatch:     "P2P Version Mismatch",
    PubkeyMissing:          "Public key missing",
    PubkeyInvalid:          "Public key invalid",
    PubkeyForbidden:        "Public key forbidden",
    ProtocolBreach:         "Protocol Breach",
    PortMismatch:           "Port mismatch",
    PingTimeout:            "Ping timeout",
    InvalidGenesis:         "Invalid genesis block",
    InvalidNetworkId:       "Invalid network id",
    InvalidProtocolVersion: "Invalid protocol version",
}

type PeerError struct {
    Code    ErrorCode
    message string
}

func NewPeerError(code ErrorCode, format string, v ...interface{}) *PeerError {
    desc, ok := errorToString[code]
    if !ok {
        panic("invalid error code")
    }
    format = desc + ": " + format
    message := fmt.Sprintf(format, v...)
    return &PeerError{code, message}
}

func (self *PeerError) Error() string {
    return self.message
}

func NewPeerErrorChannel() chan *PeerError {
    return make(chan *PeerError, errorChanCapacity)
}