improbable-eng / ts-protoc-gen

Protocol Buffers Compiler (protoc) plugin for TypeScript and gRPC-Web.
Apache License 2.0
1.36k stars 173 forks source link

Error: 13 INTERNAL: Request message serialization failure: Expected argument of type xxx #290

Closed j92z closed 2 years ago

j92z commented 2 years ago

Versions of relevant software used ts-protoc-gen: 0.15.0 nodejs: v14.16.1 What happened

import * as grpc from '@grpc/grpc-js';
import { CheckSensitiveWordRequest } from "./sensitiveCheck_pb";
import { SensitiveWordClient } from "./sensitiveCheck_grpc_pb";
export class SensitiveCheckGrpc {
    private sensitiveCheckProtoClient: SensitiveWordClient;
    constructor () {
        const uri = `${process.env.GRPC_SW_HOST}:${process.env.GRPC_SW_PORT}`
        this.sensitiveCheckProtoClient = new SensitiveWordClient(uri, grpc.credentials.createInsecure());
    }
    async checkWords(words: string[]) {
        if (!Array.isArray(words)) words = [words];
        const req = new CheckSensitiveWordRequest();
        req.setWordsList(words)
        try {
            const response = await new Promise((resolve, reject) => {
                this.sensitiveCheckProtoClient.checkSensitiveWord(req, (err, res) => {
                    if (err) {
                        reject(err);
                    } else {
                        resolve(res.toObject().resultsList);
                    }
                })
            })
            // console.log(response)
            return response;
        } catch (err) {
            console.log(err)
        }
        return [];
    }

}

when check CheckSensitiveWordRequest, throw type err

Error: 13 INTERNAL: Request message serialization failure: Expected argument of type sensitive_word_rpc.CheckSensitiveWordRequest

in sensitiveCheck_grpc_pb.js serialize function

function serialize_sensitive_word_rpc_CheckSensitiveWordRequest(arg) {
  if (!(arg instanceof src_grpc_sensitiveCheckGrpc_sensitiveCheck_pb.CheckSensitiveWordRequest)) {
    throw new Error('Expected argument of type sensitive_word_rpc.CheckSensitiveWordRequest');
  }
  return Buffer.from(arg.serializeBinary());
}

What you expected to happen (arg instanceof src_grpc_sensitiveCheckGrpc_sensitiveCheck_pb.CheckSensitiveWordRequest) == true How to reproduce it (as minimally and precisely as possible): this is what gennerate script

#!/bin/zsh
PROTOFILE="$1"
protoc \
    --plugin="protoc-gen-ts=./node_modules/.bin/protoc-gen-ts" \
    --plugin=protoc-gen-grpc="./node_modules/.bin/grpc_tools_node_protoc_plugin" \
    --js_out="import_style=commonjs,binary:./" \
    --ts_out="service=grpc-node,mode=grpc-js:./" \
    --grpc_out="grpc-js:./" \
    $PROTOFILE

Full logs to relevant components

Anything else we need to know

j92z commented 2 years ago

this question is caused by script excute path and file path

bRRRITSCOLD commented 2 years ago

@j92z i am Running into the same issue now, I have to comment out the instanceof check to make it work for now. What did you mean by "the question came because of the path of the executable"? How did you fix this?

kevinohara80 commented 1 year ago

I'm hitting this issue a lot. I also don't understand how to fix this. @j92z can you explain what you meant?