Merge pull request #55 from filecoin-project/feat/rpcc-wrap-unmarshal-err

jsonrpc: Wrap result unmarshal errors
This commit is contained in:
Whyrusleeping 2019-07-18 09:50:27 -07:00 committed by GitHub
commit 92f00b7b75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
"golang.org/x/xerrors"
) )
var log = logging.Logger("rpc") var log = logging.Logger("rpc")
@ -183,7 +184,7 @@ func NewClient(addr string, namespace string, handler interface{}) (ClientCloser
if resp.Result != nil { if resp.Result != nil {
log.Debugw("rpc result", "type", ftyp.Out(valOut)) log.Debugw("rpc result", "type", ftyp.Out(valOut))
if err := json.Unmarshal(resp.Result, rval.Interface()); err != nil { if err := json.Unmarshal(resp.Result, rval.Interface()); err != nil {
return processError(err) return processError(xerrors.Errorf("unmarshaling result: %w", err))
} }
} }
} }

View File

@ -7,6 +7,8 @@ import (
"fmt" "fmt"
"io" "io"
"reflect" "reflect"
"golang.org/x/xerrors"
) )
type rpcHandler struct { type rpcHandler struct {
@ -101,7 +103,7 @@ func (h handlers) handleReader(ctx context.Context, r io.Reader, w io.Writer, rp
var req request var req request
if err := json.NewDecoder(r).Decode(&req); err != nil { if err := json.NewDecoder(r).Decode(&req); err != nil {
rpcError(wf, &req, rpcParseError, err) rpcError(wf, &req, rpcParseError, xerrors.Errorf("unmarshaling request: %w", err))
return return
} }
@ -131,7 +133,7 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer
for i := 0; i < handler.nParams; i++ { for i := 0; i < handler.nParams; i++ {
rp := reflect.New(handler.paramReceivers[i]) rp := reflect.New(handler.paramReceivers[i])
if err := json.NewDecoder(bytes.NewReader(req.Params[i].data)).Decode(rp.Interface()); err != nil { if err := json.NewDecoder(bytes.NewReader(req.Params[i].data)).Decode(rp.Interface()); err != nil {
rpcError(w, &req, rpcParseError, err) rpcError(w, &req, rpcParseError, xerrors.Errorf("unmarshaling params for '%s': %w", handler.handlerFunc, err))
return return
} }

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"net/http/httptest" "net/http/httptest"
"strconv" "strconv"
"strings"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -179,7 +180,7 @@ func TestRPC(t *testing.T) {
} }
err = wrongtype.Add("not an int") err = wrongtype.Add("not an int")
if err == nil || err.Error() != "RPC error (-32700): json: cannot unmarshal string into Go value of type int" { if err == nil || !strings.Contains(err.Error(), "RPC error (-32700):") || !strings.Contains(err.Error(), "json: cannot unmarshal string into Go value of type int") {
t.Error("wrong error:", err) t.Error("wrong error:", err)
} }
closer() closer()