grpc / grpc/grpc-node

Unary call throws "Cannot read properties of undefined (reading 'interceptors')"

Open
#2,215 1 comment 1 reaction 0 assignees View on GitHub
package: @grpc/grpc-js
Dominant language
TypeScript
Stars
4.8k
Forks
716
Avg merge
2d 3h
Merged PRs (30d)
10

Description

### Problem description

When making unary call and passing metadata as `undefined`, the library throws:

```
TypeError: Cannot read properties of undefined (reading 'interceptors')
```

This error doesn't happen when metadata is provided.

### Reproduction steps

The client code when generated using [ts-proto](https://github.com/stephenh/ts-proto) and configuring it with `--ts_proto_opt=addGrpcMetadata=true`, the following is generated:

```
echo(request: EchoRequest, metadata?: Metadata): Observable {
return this.client.echo(request, metadata);
}
```

When code above is executed without passing a metadata, the `TypeError` is always thrown if the grpc call is of type UNARY.

### Additional context

I managed to track the issue down to [InterceptorArguments](https://github.com/grpc/grpc-node/blob/master/packages/grpc-js/src/client.ts#L301):

```
const interceptorArgs: InterceptorArguments = {
clientInterceptors: this[INTERCEPTOR_SYMBOL],
clientInterceptorProviders: this[INTERCEPTOR_PROVIDER_SYMBOL],
callInterceptors: callProperties.callOptions.interceptors ?? [],
callInterceptorProviders:
callProperties.callOptions.interceptor_providers ?? [],
};
```

The setting of `callInterceptors` assume that `callProperties.callOptions` is never undefined. The fix could be made at [checkOptionalUnaryResponseArguments L:214](https://github.com/grpc/grpc-node/blob/master/packages/grpc-js/src/client.ts#L214):

```
return { metadata: new Metadata(), options: arg1 || {}, callback: arg2 };
```

Maybe worth to check also fix L:226:

```
return { metadata: arg1, options: arg2 || {}, callback: arg3 };
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.