Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

Map gRPC error

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(),
    };
  },
);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Map #gRPC #error
ADD COMMENT
Topic
Name
9+2 =