peer_error.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package p2p
  17. import (
  18. "fmt"
  19. )
  20. const (
  21. errInvalidMsgCode = iota
  22. errInvalidMsg
  23. )
  24. var errorToString = map[int]string{
  25. errInvalidMsgCode: "invalid message code",
  26. errInvalidMsg: "invalid message",
  27. }
  28. type peerError struct {
  29. code int
  30. message string
  31. }
  32. func newPeerError(code int, format string, v ...interface{}) *peerError {
  33. desc, ok := errorToString[code]
  34. if !ok {
  35. panic("invalid error code")
  36. }
  37. err := &peerError{code, desc}
  38. if format != "" {
  39. err.message += ": " + fmt.Sprintf(format, v...)
  40. }
  41. return err
  42. }
  43. func (self *peerError) Error() string {
  44. return self.message
  45. }
  46. type DiscReason uint
  47. const (
  48. DiscRequested DiscReason = iota
  49. DiscNetworkError
  50. DiscProtocolError
  51. DiscUselessPeer
  52. DiscTooManyPeers
  53. DiscAlreadyConnected
  54. DiscIncompatibleVersion
  55. DiscInvalidIdentity
  56. DiscQuitting
  57. DiscUnexpectedIdentity
  58. DiscSelf
  59. DiscReadTimeout
  60. DiscSubprotocolError = 0x10
  61. )
  62. var discReasonToString = [...]string{
  63. DiscRequested: "Disconnect requested",
  64. DiscNetworkError: "Network error",
  65. DiscProtocolError: "Breach of protocol",
  66. DiscUselessPeer: "Useless peer",
  67. DiscTooManyPeers: "Too many peers",
  68. DiscAlreadyConnected: "Already connected",
  69. DiscIncompatibleVersion: "Incompatible P2P protocol version",
  70. DiscInvalidIdentity: "Invalid node identity",
  71. DiscQuitting: "Client quitting",
  72. DiscUnexpectedIdentity: "Unexpected identity",
  73. DiscSelf: "Connected to self",
  74. DiscReadTimeout: "Read timeout",
  75. DiscSubprotocolError: "Subprotocol error",
  76. }
  77. func (d DiscReason) String() string {
  78. if len(discReasonToString) < int(d) {
  79. return fmt.Sprintf("Unknown Reason(%d)", d)
  80. }
  81. return discReasonToString[d]
  82. }
  83. func (d DiscReason) Error() string {
  84. return d.String()
  85. }
  86. func discReasonForError(err error) DiscReason {
  87. if reason, ok := err.(DiscReason); ok {
  88. return reason
  89. }
  90. peerError, ok := err.(*peerError)
  91. if ok {
  92. switch peerError.code {
  93. case errInvalidMsgCode, errInvalidMsg:
  94. return DiscProtocolError
  95. default:
  96. return DiscSubprotocolError
  97. }
  98. }
  99. return DiscSubprotocolError
  100. }