diff --git a/lib/jsonrpc/client.go b/lib/jsonrpc/client.go index d03600f86..d065c6a2c 100644 --- a/lib/jsonrpc/client.go +++ b/lib/jsonrpc/client.go @@ -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)) } } } diff --git a/lib/jsonrpc/handler.go b/lib/jsonrpc/handler.go index 3c5e31324..97607dc50 100644 --- a/lib/jsonrpc/handler.go +++ b/lib/jsonrpc/handler.go @@ -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 } diff --git a/lib/jsonrpc/rpc_test.go b/lib/jsonrpc/rpc_test.go index f52d71792..053b89ba3 100644 --- a/lib/jsonrpc/rpc_test.go +++ b/lib/jsonrpc/rpc_test.go @@ -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()