github.com/0xPolygon/supernets2-node@v0.0.0-20230711153321-2fe574524eaa/jsonrpc/types/errors.go (about)

     1  package types
     2  
     3  import "fmt"
     4  
     5  const (
     6  	// DefaultErrorCode rpc default error code
     7  	DefaultErrorCode = -32000
     8  	// RevertedErrorCode error code for reverted txs
     9  	RevertedErrorCode = 3
    10  	// InvalidRequestErrorCode error code for invalid requests
    11  	InvalidRequestErrorCode = -32600
    12  	// NotFoundErrorCode error code for not found objects
    13  	NotFoundErrorCode = -32601
    14  	// InvalidParamsErrorCode error code for invalid parameters
    15  	InvalidParamsErrorCode = -32602
    16  	// ParserErrorCode error code for parsing errors
    17  	ParserErrorCode = -32700
    18  )
    19  
    20  // Error interface
    21  type Error interface {
    22  	Error() string
    23  	ErrorCode() int
    24  	ErrorData() *[]byte
    25  }
    26  
    27  // RPCError represents an error returned by a JSON RPC endpoint.
    28  type RPCError struct {
    29  	err  string
    30  	code int
    31  	data *[]byte
    32  }
    33  
    34  // NewRPCError creates a new error instance to be returned by the RPC endpoints
    35  func NewRPCError(code int, err string, args ...interface{}) *RPCError {
    36  	return NewRPCErrorWithData(code, err, nil, args...)
    37  }
    38  
    39  // NewRPCErrorWithData creates a new error instance with data to be returned by the RPC endpoints
    40  func NewRPCErrorWithData(code int, err string, data *[]byte, args ...interface{}) *RPCError {
    41  	var errMessage string
    42  	if len(args) > 0 {
    43  		errMessage = fmt.Sprintf(err, args...)
    44  	} else {
    45  		errMessage = err
    46  	}
    47  	return &RPCError{code: code, err: errMessage, data: data}
    48  }
    49  
    50  // Error returns the error message.
    51  func (e *RPCError) Error() string {
    52  	return e.err
    53  }
    54  
    55  // ErrorCode returns the error code.
    56  func (e *RPCError) ErrorCode() int {
    57  	return e.code
    58  }
    59  
    60  // ErrorData returns the error data.
    61  func (e *RPCError) ErrorData() *[]byte {
    62  	return e.data
    63  }