jsonrpc: better server errors on unmarshal

This commit is contained in:
Łukasz Magiera 2019-07-18 12:54:26 +02:00
parent e32d7aee12
commit 2b651ad04d
2 changed files with 5 additions and 3 deletions

View File

@ -184,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(xerrors.Errorf("unmarshaling result: ", 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
} }