diff options
author | zelig <viktor.tron@gmail.com> | 2014-10-23 23:57:54 +0800 |
---|---|---|
committer | zelig <viktor.tron@gmail.com> | 2014-10-23 23:57:54 +0800 |
commit | 771fbcc02e6d10cdf4cda2e8ec8ea23f11066feb (patch) | |
tree | 15a966dbe15e2f8388f69b396e613c7759b06f6d /p2p/peer_error.go | |
parent | 119c5b40a7ed1aea1c871c0cb56956b8ef9303d9 (diff) | |
download | dexon-771fbcc02e6d10cdf4cda2e8ec8ea23f11066feb.tar.gz dexon-771fbcc02e6d10cdf4cda2e8ec8ea23f11066feb.tar.zst dexon-771fbcc02e6d10cdf4cda2e8ec8ea23f11066feb.zip |
initial commit of p2p package
Diffstat (limited to 'p2p/peer_error.go')
-rw-r--r-- | p2p/peer_error.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/p2p/peer_error.go b/p2p/peer_error.go new file mode 100644 index 000000000..de921878a --- /dev/null +++ b/p2p/peer_error.go @@ -0,0 +1,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) +} |