updated json response to match spec by @davebryson

This commit is contained in:
Dave Bryson
2017-05-26 14:46:33 +02:00
committed by Ethan Buchman
parent e0017c8a97
commit 60a1f49a5c
6 changed files with 97 additions and 33 deletions

View File

@ -8,6 +8,11 @@ import (
events "github.com/tendermint/tmlibs/events"
)
type RpcError struct {
Code int `json:"code"`
Message string `json:"message"`
}
type RPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID string `json:"id"`
@ -50,28 +55,32 @@ func ArrayToRequest(id string, method string, params []interface{}) (RPCRequest,
type RPCResponse struct {
JSONRPC string `json:"jsonrpc"`
ID string `json:"id"`
Result *json.RawMessage `json:"result"`
Error string `json:"error"`
ID string `json:"id,omitempty"`
Result *json.RawMessage `json:"result,omitempty"`
Error *RpcError `json:"error,omitempty"`
}
func NewRPCResponse(id string, res interface{}, err string) RPCResponse {
func NewRPCSuccessResponse(id string, res interface{}) RPCResponse {
var raw *json.RawMessage
if res != nil {
var js []byte
js, err2 := json.Marshal(res)
if err2 == nil {
rawMsg := json.RawMessage(js)
raw = &rawMsg
} else {
err = err2.Error()
js, err := json.Marshal(res)
if err != nil {
return RPCInternalError(id)
}
rawMsg := json.RawMessage(js)
raw = &rawMsg
}
return RPCResponse{JSONRPC: "2.0", ID: id, Result: raw}
}
func NewRPCErrorResponse(id string, code int, msg string) RPCResponse {
return RPCResponse{
JSONRPC: "2.0",
ID: id,
Result: raw,
Error: err,
Error: &RpcError{Code: code, Message: msg},
}
}
@ -83,6 +92,30 @@ func (resp RPCResponse) String() string {
}
}
func RPCParseError(id string) RPCResponse {
return NewRPCErrorResponse(id, -32700, "Parse error. Invalid JSON")
}
func RPCInvalidRequestError(id string) RPCResponse {
return NewRPCErrorResponse(id, -32600, "Invalid Request")
}
func RPCMethodNotFoundError(id string) RPCResponse {
return NewRPCErrorResponse(id, -32601, "Method not found")
}
func RPCInvalidParamsError(id string) RPCResponse {
return NewRPCErrorResponse(id, -32602, "Invalid params")
}
func RPCInternalError(id string) RPCResponse {
return NewRPCErrorResponse(id, -32603, "Internal error")
}
func RPCServerError(id string) RPCResponse {
return NewRPCErrorResponse(id, -32000, "Server error")
}
//----------------------------------------
// *wsConnection implements this interface.

View File

@ -0,0 +1,32 @@
package rpctypes
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
type SampleResult struct {
Value string
}
func TestResponses(t *testing.T) {
assert := assert.New(t)
a := NewRPCSuccessResponse("1", &SampleResult{"hello"})
b, _ := json.Marshal(a)
s := `{"jsonrpc":"2.0","id":"1","result":{"Value":"hello"}}`
assert.Equal(string(s), string(b))
d := RPCParseError("1")
e, _ := json.Marshal(d)
f := `{"jsonrpc":"2.0","id":"1","error":{"code":-32700,"message":"Parse error. Invalid JSON"}}`
assert.Equal(string(f), string(e))
g := RPCMethodNotFoundError("2")
h, _ := json.Marshal(g)
i := `{"jsonrpc":"2.0","id":"2","error":{"code":-32601,"message":"Method not found"}}`
assert.Equal(string(h), string(i))
}