猿问

grpc:服务器无法编码响应:rpc错误:代码=内部desc = grpc:编组时出错:

理想情况下,以下 RPC 应该接收消息并编组为 JSON。但是,遇到以下错误:ERROR: 2018/08/12 13:43:07 grpc: server failed to encode response:  rpc error: code = Internal desc = grpc: error while marshaling: proto: Marshal called with nil


func (s *beaconServer) Transmit(ctx context.Context, batch *pb.Batch) (*pb.Empty, error) {

    var empty *pb.Empty


    var messageJSON bytes.Buffer

    marshaler := &jsonpb.Marshaler{

        OrigName: true,

    }

    err := marshaler.Marshal(&messageJSON, batch)

    if err != nil {

        return empty, err

    }


    log.Println(string(messageJSON.Bytes()))

    return empty, nil

}

.. 回报


2018/08/12 14:24:09 beacon.go:34: {"stream_id":"abc11","event_type":"e","events":[{"file_path":"/tmp/python.py","location":"256","count":"30"},{"file_path":"/tmp/temp.py","location":"253","count":"305"}],"start_time":"2038-01-19 03:14:07","end_time":"2038-01-19 03:14:27"}

ERROR: 2018/08/12 14:24:09 grpc: server failed to encode response:  rpc error: code = Internal desc = grpc: error while marshaling: proto: Marshal called with nil


米脂
浏览 865回答 1
1回答

千万里不及你

该错误可能不是由此代码块中的编组器引起的当您这样做时,var empty *pb.Empty您正在创建一个未初始化的变量*pb.Empty,即nil. 很可能是上游试图整理它的东西empty导致了错误。如果你想初始化为空,那么你应该做return new(pb.Empty), nil
随时随地看视频慕课网APP

相关分类

Go
我要回答