Unary call throws "Cannot read properties of undefined (reading 'interceptors')"
- 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
Assessment
This issue has not been assessed yet.