github.com/karalabe/go-ethereum@v0.8.5/eth/error.go (about)

     1  package eth
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  const (
     8  	ErrMsgTooLarge = iota
     9  	ErrDecode
    10  	ErrInvalidMsgCode
    11  	ErrProtocolVersionMismatch
    12  	ErrNetworkIdMismatch
    13  	ErrGenesisBlockMismatch
    14  	ErrNoStatusMsg
    15  	ErrExtraStatusMsg
    16  	ErrInvalidBlock
    17  	ErrInvalidPoW
    18  	ErrUnrequestedBlock
    19  	ErrInsufficientChainInfo
    20  )
    21  
    22  var errorToString = map[int]string{
    23  	ErrMsgTooLarge:             "Message too long",
    24  	ErrDecode:                  "Invalid message",
    25  	ErrInvalidMsgCode:          "Invalid message code",
    26  	ErrProtocolVersionMismatch: "Protocol version mismatch",
    27  	ErrNetworkIdMismatch:       "NetworkId mismatch",
    28  	ErrGenesisBlockMismatch:    "Genesis block mismatch",
    29  	ErrNoStatusMsg:             "No status message",
    30  	ErrExtraStatusMsg:          "Extra status message",
    31  	ErrInvalidBlock:            "Invalid block",
    32  	ErrInvalidPoW:              "Invalid PoW",
    33  	ErrUnrequestedBlock:        "Unrequested block",
    34  	ErrInsufficientChainInfo:   "Insufficient chain info",
    35  }
    36  
    37  type protocolError struct {
    38  	Code    int
    39  	fatal   bool
    40  	message string
    41  	format  string
    42  	params  []interface{}
    43  	// size    int
    44  }
    45  
    46  func newProtocolError(code int, format string, params ...interface{}) *protocolError {
    47  	return &protocolError{Code: code, format: format, params: params}
    48  }
    49  
    50  func ProtocolError(code int, format string, params ...interface{}) (err *protocolError) {
    51  	err = newProtocolError(code, format, params...)
    52  	// report(err)
    53  	return
    54  }
    55  
    56  func (self protocolError) Error() (message string) {
    57  	if len(message) == 0 {
    58  		var ok bool
    59  		self.message, ok = errorToString[self.Code]
    60  		if !ok {
    61  			panic("invalid error code")
    62  		}
    63  		if self.format != "" {
    64  			self.message += ": " + fmt.Sprintf(self.format, self.params...)
    65  		}
    66  	}
    67  	return self.message
    68  }
    69  
    70  func (self *protocolError) Fatal() bool {
    71  	return self.fatal
    72  }