diff options
author | zelig <viktor.tron@gmail.com> | 2014-12-06 05:14:55 +0800 |
---|---|---|
committer | zelig <viktor.tron@gmail.com> | 2014-12-15 04:27:05 +0800 |
commit | e5aa38cb0f846cde3e0d70e751cfa6d53a889e99 (patch) | |
tree | 90e6b057ca12d18eca5c5058df84dfd7558e7bdd /eth/error.go | |
parent | f8061fcba8648593e03ce3d847613d8c8e0f4797 (diff) | |
download | dexon-e5aa38cb0f846cde3e0d70e751cfa6d53a889e99.tar.gz dexon-e5aa38cb0f846cde3e0d70e751cfa6d53a889e99.tar.zst dexon-e5aa38cb0f846cde3e0d70e751cfa6d53a889e99.zip |
initial commit for eth-p2p integration
Diffstat (limited to 'eth/error.go')
-rw-r--r-- | eth/error.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/eth/error.go b/eth/error.go new file mode 100644 index 000000000..cb4459435 --- /dev/null +++ b/eth/error.go @@ -0,0 +1,73 @@ +package eth + +import ( + "fmt" + // "github.com/ethereum/go-ethereum/logger" +) + +const ( + ErrMsgTooLarge = iota + ErrDecode + ErrInvalidMsgCode + ErrProtocolVersionMismatch + ErrNetworkIdMismatch + ErrGenesisBlockMismatch + ErrNoStatusMsg + ErrExtraStatusMsg + ErrInvalidBlock +) + +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", +} + +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) + if err.Fatal() { + logger.Errorln(err) + } else { + logger.Debugln(err) + } + return +} + +func (self protocolError) Error() (message string) { + message = self.message + if message == "" { + message, ok := errorToString[self.Code] + if !ok { + panic("invalid error code") + } + if self.format != "" { + message += ": " + fmt.Sprintf(self.format, self.params...) + } + self.message = message + } + return +} + +func (self *protocolError) Fatal() bool { + return self.fatal +} |