nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.29k stars 7.59k forks source link

ArgumentsHost Microservices can't getHandler and getClass #8477

Closed pergerk closed 2 years ago

pergerk commented 2 years ago

Is there an existing issue for this?

Current behavior

Hello, I am trying to getClass and getHandler under rpc but functions are undefined? And I doing it wrong or do we have any case to solve this ?

const rpc = host.switchToRpc(); const ctx = rpc.getContext(); console.log('host.getType()', host.getType()); console.log('ctx.getClass fn', ctx.getClass); console.log('ctx.getHandler fn', ctx.getHandler); ctx.getClass and ctx.getHandler are undefined under rpc scope

Minimum reproduction code

https://github.com/pergerk/microservices

Steps to reproduce

npm i npm run start:dev gw & npm run start:dev service1 & npm run start:dev service2 curl http://localhost:3000/service1

Expected behavior

const ctx = rpc.getContext(); should return name of class ctx.getClass().name should return name of handler ctx.getHandler().name

Package

Other package

No response

NestJS version

8.1.2

Packages versions

"@grpc/grpc-js": "^1.4.2",
"@grpc/proto-loader": "^0.6.6",
"@nestjs/common": "^8.1.2",
"@nestjs/core": "^8.1.2",
"@nestjs/microservices": "^8.1.2",
"@nestjs/platform-express": "^8.1.2",
"@nestjs/swagger": "^5.0.0",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",

Node.js version

v16.5.0

In which operating systems have you tested?

Other

[Nest] 89073 - 31.10.2021, 05:59:43 LOG [NestMicroservice] Nest microservice successfully started +12ms callone called host.getType() rpc ctx.getClass fn undefined ctx.getHandler fn undefined filtered instance rpc Error in service 1

jmcdo29 commented 2 years ago

An ArgumentHost has never been able to get the class or handler, because ArgumentHost is used in an ExceptionFilter where there may not be a matching class or route handler (404s and such). Only the ExecutionContext has the getClass() and getHandler() methods. And the ExecutionContext is different than the host.switchToRcp().getContext() return object.