I have Two nestjs project for TCP based microservice, first is projectone which is main server or gateway for microservices. Second is projectwo which is first microservice or client. When i call a service from main server to access the data from client, the dto file throws an bad exception error, which is okay, dto file is working well in client/microservice.
But the issue is that main server or project one is not catching the error thrown by the client/microservice. it return empty error object and throws 500 internal server everytime, not matter which error is thrown by the client/microservice. here is the piece of code.
Projectone:
/app.controller.ts
async createUser(@Body() data: object): Promise<object> {
return this.appService.createNewUser(data);
}
*/app.service.ts*
@Injectable()
export class AppService {
public readonly client: ClientProxy;
constructor() {
this.client = ClientProxyFactory.create({
transport: Transport.TCP,
options: {
port: 5001,
},
});
}
async createNewUser(data: any): Promise<any> {
return this.client.send<any>('user', data).pipe(catchError(error => throwError(() => {
console.log(error);
// return new HttpException(error.response, HttpStatus.BAD_REQUEST);
})));
}
}
(Return empty error object)
main.ts
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));
await app.listen(5000);
Projectwo:
*app.controller.ts*
@MessagePattern('user')
@UseFilters(HttpExceptionFilter)
async create(@Payload() createUserDto: CreateUserDto): Promise<usermain> {
console.log('Reaching....');
return await this.usersService.createUser(createUserDto);
}
*app.service.ts*
public async createUser(data: CreateUserDto): Promise<usermain> {
console.log('Data~~~~~>', data);
const result = await this.prisma.usermain.create({
data,
});
return result;
}
exception-filter.ts
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const context = host.switchToHttp();
const response = context.getResponse<Response>();
const request = context.getRequest<Request>();
const status = exception.getStatus();
console.log("EXCEPTION OBJECT-----> ", exception.getStatus());
response
.status(status)
.json({
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
message: exception.message
});
}
}
how can i catch error object by client/microservice in main server project?