go-ethereum/eth/error.go

73 lines
1.7 KiB
Go
Raw Normal View History

2014-12-05 15:14:55 -06:00
package eth
import (
"fmt"
)
const (
ErrMsgTooLarge = iota
ErrDecode
ErrInvalidMsgCode
ErrProtocolVersionMismatch
ErrNetworkIdMismatch
ErrGenesisBlockMismatch
ErrNoStatusMsg
ErrExtraStatusMsg
ErrInvalidBlock
ErrInvalidPoW
ErrUnrequestedBlock
2015-01-09 00:03:32 -06:00
ErrInsufficientChainInfo
2014-12-05 15:14:55 -06:00
)
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",
2015-01-09 00:03:32 -06:00
ErrInsufficientChainInfo: "Insufficient chain info",
2014-12-05 15:14:55 -06:00
}
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) {
2015-01-05 10:10:42 -06:00
if len(message) == 0 {
var ok bool
self.message, ok = errorToString[self.Code]
2014-12-05 15:14:55 -06:00
if !ok {
panic("invalid error code")
}
if self.format != "" {
2015-01-05 10:10:42 -06:00
self.message += ": " + fmt.Sprintf(self.format, self.params...)
2014-12-05 15:14:55 -06:00
}
}
2015-01-05 10:10:42 -06:00
return self.message
2014-12-05 15:14:55 -06:00
}
func (self *protocolError) Fatal() bool {
return self.fatal
}