I am trying to build a NestJS backend with multiple microservices and one REST API as a Gateway which communicates with the microservices. For communication between the Gateway and Microservices I am using gRPC. Simple communication is already working, but now I wanted to implement Error Handling in the Microservices. The NestJS Documentation states that this is possible with the RpcException class.https://docs.nestjs.com/microservices/exception-filters But if I try to catch the Exception in the Gateway API I only get "ERROR [ExceptionsHandler] 2 UNKNOWN: ..." followed by the Exceptions error message.
Gateway API: user.controller.ts
import { Controller, Get, Param } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { UserViewModel } from '../../proto/build/service-name/user';
import { UserService } from './user.service';
@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
@Get(':id')
async getUserById(@Param('id') id: number): Promise<UserViewModel> {
try {
return await this.userService.getUserById(id);
} catch (error) {
return error;
}
}
}
Gateway API: user.service.ts
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
import { ClientGrpc } from '@nestjs/microservices';
import {
IUserService,
UserViewModel,
} from '../../proto/build/service-name/user';
@Injectable()
export class UserService implements OnModuleInit {
private grpcService: IUserService;
constructor(@Inject('USER_PACKAGE') private client: ClientGrpc) {}
onModuleInit() {
this.grpcService = this.client.getService<IUserService>('IUserService');
}
async getUserById(id: number): Promise<UserViewModel> {
return this.grpcService.getUserById({ id });
}
}
Microservice: user.controller.ts
import { Metadata } from '@grpc/grpc-js';
import { Controller } from '@nestjs/common';
import { GrpcMethod, RpcException } from '@nestjs/microservices';
import { User } from './../../node_modules/.prisma/client/index.d';
import { PrismaService } from '../prisma/prisma.service';
import { UserViewModel, GetUserById } from '../proto/build/service-name/user';
@Controller()
export class UserController {
constructor(private readonly prisma: PrismaService) {}
@GrpcMethod('IUserService', 'getUserById')
async getUserById(
data: GetUserById,
metadata: Metadata,
): Promise<UserViewModel> {
const user: User = await this.prisma.user.findFirst({
where: { id: { equals: data.id } },
});
if (!user) {
throw new RpcException('User not found');
}
return { name: user.name, email: user.email };
}
}
I ran into this issue as well when building microservices with an API gateway. The solution I came up with is a combination of the answers I've found here but allows you to use the build in NestJS exceptions.
So basically I wrap the built in NestJS HTTP exception with the
RpcException
in the microservice. Then you can catch the execption in your api gateway and handle it with a filter. TheRcpException
message can be either astring
orobject
, this allows you to pass the built in HTTP exceptions (NotFoundException
,UnauthorizedException
, etc) as a message so you don't have to deal with status codes.Microservice
Gateway Controller
Exception Filter
Register Filter