Warn on non zero result with error

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-03-14 15:08:24 +01:00
parent 21b34ba133
commit 06ce4f21c5
2 changed files with 14 additions and 5 deletions

View File

@ -231,12 +231,16 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer
} }
} }
var kind reflect.Kind
var res interface{} var res interface{}
var nonZero bool
if handler.valOut != -1 { if handler.valOut != -1 {
res = callResult[handler.valOut].Interface() res = callResult[handler.valOut].Interface()
kind = callResult[handler.valOut].Kind()
nonZero = !callResult[handler.valOut].IsZero()
} }
if res != nil && reflect.TypeOf(res).Kind() == reflect.Chan { if res != nil && kind == reflect.Chan {
// Channel responses are sent from channel control goroutine. // Channel responses are sent from channel control goroutine.
// Sending responses here could cause deadlocks on writeLk, or allow // Sending responses here could cause deadlocks on writeLk, or allow
// sending channel messages before this rpc call returns // sending channel messages before this rpc call returns
@ -253,12 +257,13 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer
Code: 1, Code: 1,
Message: err.(error).Error(), Message: err.(error).Error(),
} }
} } else if resp.Error == nil {
// check error as JSON-RPC spec prohibits error and value at the same time
// check error as JSON-RPC spec prohibits error and value at the same time
if resp.Error == nil {
resp.Result = res resp.Result = res
} }
if resp.Error != nil && nonZero {
log.Errorw("error and res returned", "request", req, "r.err", resp.Error, "res", res)
}
w(func(w io.Writer) { w(func(w io.Writer) {
if err := json.NewEncoder(w).Encode(resp); err != nil { if err := json.NewEncoder(w).Encode(resp); err != nil {

View File

@ -19,6 +19,10 @@ func (p *param) UnmarshalJSON(raw []byte) error {
} }
func (p *param) MarshalJSON() ([]byte, error) { func (p *param) MarshalJSON() ([]byte, error) {
if p.v.Kind() == reflect.Invalid {
return p.data, nil
}
return json.Marshal(p.v.Interface()) return json.Marshal(p.v.Interface())
} }