grpc / grpc-web

gRPC for Web Clients
https://grpc.io
Apache License 2.0
8.45k stars 760 forks source link

Getting Error: Assertion failed in reactjs request #1435

Closed rizdaputra closed 2 weeks ago

rizdaputra commented 2 weeks ago

I have been trying to create a client using the generated js from a proto file that i have. This is the proto file example

service Database {
  rpc get_rows(rows_request) returns (rows_return);
}

message filter
    {
        TableNames table = 1; 
        string field_name = 2;
        DbOperatorType operation_type = 3;
        repeated string filter_value = 4;
    }

    message rows_request
    {
        TableNames table = 1; ///name of the table
      uint32 limit = 2; /// number of max results
      uint32 offset = 3; /// offset the results
      repeated filter filter = 4; 
      repeated OrderBy order = 5;   
  }

enum DbOperatorType
    {
        DB_EQUAL=0;
        DB_NOT_EQUAL=1;
        DB_LESS=2;
        DB_LESS_OR_EQUAL=3;
        DB_GREATER_THAN=4;
        DB_GREATER_THAN_OR_EQUAL=5;
        DB_BETWEEN=6;
        DB_NOT_BETWEEN=7;
        DB_ILIKE=8;
        DB_NOT_ILIKE=9;
        DB_IN=10;
        DB_NOT_IN=11;
    }

message OrderBy
    {
        string name = 1;  /// field name
        OrderByType type = 2; /// type of order ASC or DSC
        TableNames table = 3; ///name of the table
    }

enum OrderByType
    {
      ASC = 0; /// Ascended order
      DSC = 1; /// Descended order
  }

etc. etc.

and this is my client

const client = new DatabasePromiseClient("http"//url", null, null);

export const getRows = (params) => {
    console.log("CALLEDDDD")
  let request = new rows_request();
    request.setTable(params.table);
  request.setLimit(params.limit);
  request.setOffset(params.offset);

  if (Array.isArray(params.filter) && params.filter.length > 0) {
        console.log("ENTER FILTER SETUP")
    params.filter.forEach( filterItem => {
      let filterRequest = new ProtoFilter();
      filterRequest.setFieldName(filterItem.fieldName);
      filterRequest.setOperationType(filterItem.operationType);
            filterItem.filterValue.forEach( filterValue => {
                filterRequest.addFilterValue(filterValue);
            })

            request.addFilter(filterRequest);
    })
  }

    if (Array.isArray(params.order) && params.order.length > 0) {
        console.log("ENTER ORDER SETUP")
        params.order.forEach( orderItem => {
            console.log(orderItem)
            let orderRequest = new OrderBy();
            orderRequest.setName(orderItem.name);
            orderRequest.setType(orderItem.type);
            orderRequest.setTable(params.table);
            request.addOrder(orderRequest);
        })
    }

    console.log(request);

    return client.get_rows(request, {});
}

whenever i call the getRows function it gives me error

Error: Assertion failed
    at __webpack_modules__../node_modules/google-protobuf/google-protobuf.js.jspb.asserts.doAssertFailure (google-protobuf.js:88:1)
    at __webpack_modules__../node_modules/google-protobuf/google-protobuf.js.jspb.asserts.assert (google-protobuf.js:88:1)
    at __webpack_modules__../node_modules/google-protobuf/google-protobuf.js.jspb.BinaryWriter.writeEnum (google-protobuf.js:470:1)
    at proto.service.rows_request.serializeBinaryToWriter (service_pb.js:7042:1)

I already checked on what i pass and they are matching with the enum that was setup. and the debugging is a bit hard since it fails even before sending the request to the server. can Anyone help me? Thanks!

sampajano commented 2 weeks ago

(Will convert this to be a discussion since this is not a bug with grpc-web itself but rather an error thrown from google-protobuf.)


@rizdaputra Hi!

It looks like from the error stack trace that the error was thrown from here:

https://github.com/protocolbuffers/protobuf-javascript/blob/5d939dbc8fd5245c4316d52c203a1bff24bac5c4/binary/writer.js#L802C29-L809

Where it's complaining the enum values are illegal:

  jspb.asserts.assert(
      (value >= -jspb.BinaryConstants.TWO_TO_31) &&
      (value < jspb.BinaryConstants.TWO_TO_31));

I see that you have 2 enums setter calls above, on setOperationType() and setType().

Could you double check the value being set on those 2 fields? Maybe they are illegal in someway.

Hope that helps!