Merge pull request #55 from filecoin-project/feat/rpcc-wrap-unmarshal-err
jsonrpc: Wrap result unmarshal errors
This commit is contained in:
commit
92f00b7b75
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user