github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/shared/types.go (about)

     1  // Copyright 2015 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  
    17  package shared
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	"github.com/ethereum/go-ethereum/logger"
    23  	"github.com/ethereum/go-ethereum/logger/glog"
    24  )
    25  
    26  // Ethereum RPC API interface
    27  type EthereumApi interface {
    28  	// API identifier
    29  	Name() string
    30  
    31  	// API version
    32  	ApiVersion() string
    33  
    34  	// Execute the given request and returns the response or an error
    35  	Execute(*Request) (interface{}, error)
    36  
    37  	// List of supported RCP methods this API provides
    38  	Methods() []string
    39  }
    40  
    41  // RPC request
    42  type Request struct {
    43  	Id      interface{}     `json:"id"`
    44  	Jsonrpc string          `json:"jsonrpc"`
    45  	Method  string          `json:"method"`
    46  	Params  json.RawMessage `json:"params"`
    47  }
    48  
    49  // RPC response
    50  type Response struct {
    51  	Id      interface{} `json:"id"`
    52  	Jsonrpc string      `json:"jsonrpc"`
    53  }
    54  
    55  // RPC success response
    56  type SuccessResponse struct {
    57  	Id      interface{} `json:"id"`
    58  	Jsonrpc string      `json:"jsonrpc"`
    59  	Result  interface{} `json:"result"`
    60  }
    61  
    62  // RPC error response
    63  type ErrorResponse struct {
    64  	Id      interface{}  `json:"id"`
    65  	Jsonrpc string       `json:"jsonrpc"`
    66  	Error   *ErrorObject `json:"error"`
    67  }
    68  
    69  // RPC error response details
    70  type ErrorObject struct {
    71  	Code    int    `json:"code"`
    72  	Message string `json:"message"`
    73  	// Data    interface{} `json:"data"`
    74  }
    75  
    76  // Create RPC error response, this allows for custom error codes
    77  func NewRpcErrorResponse(id interface{}, jsonrpcver string, errCode int, err error) *interface{} {
    78  	var response interface{}
    79  
    80  	jsonerr := &ErrorObject{errCode, err.Error()}
    81  	response = ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr}
    82  
    83  	glog.V(logger.Detail).Infof("Generated error response: %s", response)
    84  	return &response
    85  }
    86  
    87  // Create RPC response
    88  func NewRpcResponse(id interface{}, jsonrpcver string, reply interface{}, err error) *interface{} {
    89  	var response interface{}
    90  
    91  	switch err.(type) {
    92  	case nil:
    93  		response = &SuccessResponse{Jsonrpc: jsonrpcver, Id: id, Result: reply}
    94  	case *NotImplementedError:
    95  		jsonerr := &ErrorObject{-32601, err.Error()}
    96  		response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr}
    97  	case *DecodeParamError, *InsufficientParamsError, *ValidationError, *InvalidTypeError:
    98  		jsonerr := &ErrorObject{-32602, err.Error()}
    99  		response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr}
   100  	default:
   101  		jsonerr := &ErrorObject{-32603, err.Error()}
   102  		response = &ErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: jsonerr}
   103  	}
   104  
   105  	glog.V(logger.Detail).Infof("Generated response: %T %s", response, response)
   106  	return &response
   107  }