gitee.com/lh-her-team/common@v1.5.1/errors/json_errors.go (about)

     1  package errors
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"gitee.com/lh-her-team/common/json"
     7  )
     8  
     9  var (
    10  	// json
    11  	// -32768 to -32000 is the reserved predefined error code
    12  	// the server received an invalid JSON. The error is sent to the server trying to parse the JSON text
    13  	ErrParseError = JsonError{Code: -32700, Message: "Parse error"}
    14  	// the sent JSON is not a valid request object
    15  	ErrInvalidRequest = JsonError{Code: -32600, Message: "Invalid request"}
    16  	// the method does not exist or is invalid
    17  	ErrMethodNotFound = JsonError{Code: -32601, Message: "Method not found"}
    18  	ErrInvalidParams  = JsonError{Code: -32602, Message: "Invalid params"} // invalid method parameter
    19  	ErrInternalError  = JsonError{Code: -32603, Message: "Internal error"} // json-rpc internal error.
    20  	// -32000 to -32099	is the server error reserved for customization
    21  
    22  	// txPool
    23  	// the object is nil
    24  	ErrStructEmpty = JsonError{Code: -31100, Message: "Struct is nil"}
    25  	// tx-id already exists
    26  	ErrTxIdExist = JsonError{Code: -31105, Message: "TxId exist"}
    27  	// tx-id already exists in DB
    28  	ErrTxIdExistDB = JsonError{Code: -31106, Message: "TxId exist in DB"}
    29  	// tx-timestamp out of range
    30  	ErrTxTimeout = JsonError{Code: -31108, Message: "TxTimestamp error"}
    31  	// transaction pool is full
    32  	ErrTxPoolLimit = JsonError{Code: -31110, Message: "TxPool is full"}
    33  	// tx-source is error
    34  	ErrTxSource = JsonError{Code: -31112, Message: "TxSource is err"}
    35  	// The tx had been on the block chain
    36  	ErrTxHadOnTheChain = JsonError{Code: -31113, Message: "The tx had been on the block chain"}
    37  	// The txPool service has stopped
    38  	ErrTxPoolHasStopped = JsonError{Code: -31114, Message: "The tx pool has stopped"}
    39  	// The txPool service has started
    40  	ErrTxPoolHasStarted = JsonError{Code: -31114, Message: "The tx pool has started"}
    41  
    42  	// core
    43  	// block had been committed
    44  	ErrBlockHadBeenCommited = JsonError{Code: -31200, Message: "Block had been committed err"}
    45  	// block concurrent verify error
    46  	ErrConcurrentVerify = JsonError{Code: -31201, Message: "Block concurrent verify err"}
    47  	// block had been verified
    48  	ErrRepeatedVerify = JsonError{Code: -31202, Message: "Block had been verified err"}
    49  
    50  	// sync
    51  	// The sync service has been started
    52  	ErrSyncServiceHasStarted = JsonError{Code: -33000, Message: "The sync service has been started"}
    53  	// The sync service has been stoped
    54  	ErrSyncServiceHasStoped = JsonError{Code: -33001, Message: "The sync service has been stoped"}
    55  
    56  	// store
    57  	// The store service has been started
    58  	ErrStoreServiceNeedRestarted = JsonError{Code: -34000, Message: "The store service need restart"}
    59  )
    60  
    61  var (
    62  	// core
    63  	// Block rw set verify fail txs
    64  	WarnRwSetVerifyFailTxs = JsonError{Code: -31203, Message: "Block rw set verify fail txs"}
    65  )
    66  
    67  type JsonError struct {
    68  	Code    int         `json:"code"`
    69  	Message string      `json:"message"`
    70  	Data    interface{} `json:"data,omitempty"`
    71  }
    72  
    73  func (err JsonError) String() string {
    74  	marshal, _ := json.Marshal(err)
    75  	return string(marshal)
    76  }
    77  
    78  func (err JsonError) Error() string {
    79  	if err.Message == "" {
    80  		return fmt.Sprintf("error %d", err.Code)
    81  	}
    82  	return err.Message
    83  }
    84  
    85  func (err JsonError) ErrorCode() int {
    86  	return err.Code
    87  }