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"
logging "github.com/ipfs/go-log"
"golang.org/x/xerrors"
)
var log = logging.Logger("rpc")
@ -183,7 +184,7 @@ func NewClient(addr string, namespace string, handler interface{}) (ClientCloser
if resp.Result != nil {
log.Debugw("rpc result", "type", ftyp.Out(valOut))
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"
"io"
"reflect"
"golang.org/x/xerrors"
)
type rpcHandler struct {
@ -101,7 +103,7 @@ func (h handlers) handleReader(ctx context.Context, r io.Reader, w io.Writer, rp
var req request
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
}
@ -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++ {
rp := reflect.New(handler.paramReceivers[i])
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
}

View File

@ -5,6 +5,7 @@ import (
"errors"
"net/http/httptest"
"strconv"
"strings"
"sync"
"testing"
"time"
@ -179,7 +180,7 @@ func TestRPC(t *testing.T) {
}
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)
}
closer()