aboutsummaryrefslogtreecommitdiffstats
path: root/eth/error.go
blob: 9c4a6848181742cc084d3232e878d9558d085a76 (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
package eth

import (
    "fmt"
)

const (
    ErrMsgTooLarge = iota
    ErrDecode
    ErrInvalidMsgCode
    ErrProtocolVersionMismatch
    ErrNetworkIdMismatch
    ErrGenesisBlockMismatch
    ErrNoStatusMsg
    ErrExtraStatusMsg
    ErrInvalidBlock
    ErrInvalidPoW
    ErrUnrequestedBlock
    ErrInsufficientChainInfo
)

var errorToString = map[int]string{
    ErrMsgTooLarge:             "Message too long",
    ErrDecode:                  "Invalid message",
    ErrInvalidMsgCode:          "Invalid message code",
    ErrProtocolVersionMismatch: "Protocol version mismatch",
    ErrNetworkIdMismatch:       "NetworkId mismatch",
    ErrGenesisBlockMismatch:    "Genesis block mismatch",
    ErrNoStatusMsg:             "No status message",
    ErrExtraStatusMsg:          "Extra status message",
    ErrInvalidBlock:            "Invalid block",
    ErrInvalidPoW:              "Invalid PoW",
    ErrUnrequestedBlock:        "Unrequested block",
    ErrInsufficientChainInfo:   "Insufficient chain info",
}

type protocolError struct {
    Code    int
    fatal   bool
    message string
    format  string
    params  []interface{}
    // size    int
}

func newProtocolError(code int, format string, params ...interface{}) *protocolError {
    return &protocolError{Code: code, format: format, params: params}
}

func ProtocolError(code int, format string, params ...interface{}) (err *protocolError) {
    err = newProtocolError(code, format, params...)
    // report(err)
    return
}

func (self protocolError) Error() (message string) {
    if len(message) == 0 {
        var ok bool
        self.message, ok = errorToString[self.Code]
        if !ok {
            panic("invalid error code")
        }
        if self.format != "" {
            self.message += ": " + fmt.Sprintf(self.format, self.params...)
        }
    }
    return self.message
}

func (self *protocolError) Fatal() bool {
    return self.fatal
}