From 2b651ad04dab63e7043ac9ebf4b55636ddbed775 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 18 Jul 2019 12:54:26 +0200 Subject: [PATCH] jsonrpc: better server errors on unmarshal --- lib/jsonrpc/client.go | 2 +- lib/jsonrpc/handler.go | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/jsonrpc/client.go b/lib/jsonrpc/client.go index 857b2b6d3..d065c6a2c 100644 --- a/lib/jsonrpc/client.go +++ b/lib/jsonrpc/client.go @@ -184,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(xerrors.Errorf("unmarshaling result: ", 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 }