Map gRPC error

The solution for “Map gRPC error” can be found here. The following code will assist you in solving the problem.

import { createParamDecorator, ExecutionContext } from ‘@nestjs/common’;
import { Metadata, status } from ‘@grpc/grpc-js’;
import { RpcException } from ‘@nestjs/microservices’;

export interface IUserDecorator {
clientId: string;
}

export const User = createParamDecorator(
(data, context: ExecutionContext): IUserDecorator => {
const metadata = context.switchToRpc().getContext() as Metadata;
const { clientid } = metadata.getMap();

if (!clientid) {
throw new RpcException({
code: status.UNAUTHENTICATED,
message: ‘clientId is required’,
});
}

return {
clientId: clientid.toString(),
};
},
);

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in