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.28k stars 7.59k forks source link

GRPC exception filter always sends UNKNOWN status code #5615

Closed hejkerooo closed 3 years ago

hejkerooo commented 3 years ago

Bug Report

Current behavior

RpcException does not work as expected when it's being used in the RpcExceptionFilter

Input Code

// Works correctly - client is receiving error with status 5 (NOT_FOUND)
@Controller()
class HeroController {
    @GrpcMethod('HeroService', 'FindHero')
    public async refreshToken(data: FindHeroRequest): Promise<FindHeroResponse> {
      throw new RpcException({ code: 5, message: 'test'});
    }
}

// Does not work as expected - Client is receiving error with code 2 (UNKNOWN)
@UseFilters(HeroExceptionFilter)
@Controller()
class HeroController {
@GrpcMethod('HeroService', 'FindHero')
  public async refreshToken(data: FindHeroRequest): Promise<FindHeroResponse> {
    throw new HeroException();
  }
}

@Catch(HeroException)
export class HeroExceptionFilter implements RpcExceptionFilter {
  public catch(exception: HeroException | Error, host: ArgumentsHost): Observable<unknown> {
    return throwError(new RpcException({
      code: 5,
      message: "test",
    }));
  }
}

Expected behavior

RpcException thrown in exception filter should behave the same as being thrown in controller

From what I can see message property is passed correctly while code is not

Possible Solution

Environment


Nest version: 7.4.4


For Tooling issues:
- Node version: 14.13.1
- Platform:  Linux

Others:

kamilmysliwiec commented 3 years ago

Please provide a minimum reproduction repository.

hejkerooo commented 3 years ago

@kamilmysliwiec

https://github.com/hejkerooo/nestjs-grpc-error

jmcdo29 commented 3 years ago

Related to #5694

kamilmysliwiec commented 3 years ago

Let's track this here #5694