diff options
Diffstat (limited to 'rpc/shared/types.go')
-rw-r--r-- | rpc/shared/types.go | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/rpc/shared/types.go b/rpc/shared/types.go index 46fd5552c..600d39541 100644 --- a/rpc/shared/types.go +++ b/rpc/shared/types.go @@ -1,6 +1,10 @@ package shared -import "encoding/json" +import ( + "encoding/json" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" +) // RPC request type Request struct { @@ -36,3 +40,24 @@ type ErrorObject struct { Message string `json:"message"` // Data interface{} `json:"data"` } + +func NewRpcResponse(id interface{}, jsonrpcver string, reply interface{}, err error) *interface{} { + var response interface{} + + switch err.(type) { + case nil: + response = &SuccessResponse{Jsonrpc: jsonrpcver, Id: id, Result: reply} + case *NotImplementedError: + jsonerr := &ErrorObject{-32601, err.Error()} + response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr} + case *DecodeParamError, *InsufficientParamsError, *ValidationError, *InvalidTypeError: + jsonerr := &ErrorObject{-32602, err.Error()} + response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr} + default: + jsonerr := &ErrorObject{-32603, err.Error()} + response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr} + } + + glog.V(logger.Detail).Infof("Generated response: %T %s", response, response) + return &response +} |