Fix Error and Result being returne at the same time in JSON-RPC

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-03-11 18:47:13 +01:00
parent a0f1c8c87f
commit 8d870a03b5

View File

@ -230,9 +230,6 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer
}
}
}
if handler.valOut != -1 {
resp.Result = callResult[handler.valOut].Interface()
}
if resp.Result != nil && reflect.TypeOf(resp.Result).Kind() == reflect.Chan {
// Channel responses are sent from channel control goroutine.
// Sending responses here could cause deadlocks on writeLk, or allow
@ -252,6 +249,11 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer
}
}
// check error as JSON-RPC spec prohibits error and value at the same time
if resp.Error == nil && handler.valOut != -1 {
resp.Result = callResult[handler.valOut].Interface()
}
w(func(w io.Writer) {
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Error(err)