4

With nestJS rabbit MQ I use RPC from one server to another one. What I basically want is await this.amqpConnection.request to reject the promise when the consumer throw an error. As I understand from this and this and this documentation when you NACK (negative ack) the message it should put the RPC response to a corresponding queue with an error information which would be passed to a client. Here's some code example:

Producer

import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';

let amqpConnection: AmqpConnection;

sendMessage(payload: any) {
  try {
      const result = await this.amqpConnection.request({
        exchange: 'user',
        routingKey: 'test',
        queue: 'test',
        payload,
      });
      console.log(result);
  } catch (e) {
      console.error(e); // I expect this flow !!! 
  }
}
    

Consumer:

  @RabbitRPC({
    exchange: 'user',
    routingKey: 'test',
    queue: 'test',
    errorBehavior: MessageHandlerErrorBehavior.NACK
  })
  public async rpcHandler(payload: any): Promise<any> {
     throw Error("Error text that should be proxied to console.error"):
  }
  

But the message doesn't even return to the client. Client gets rejected after a timeout, rather because of an error during consumer processing. If my understanding is not correct I would like to know if there's a builtin mechanism with nestjs to reject the message w/o manually creating some response types for an error and handling it on a client.

deathangel908
  • 8,601
  • 8
  • 47
  • 81

1 Answers1

0

You should throw exactly the RpcException from @nestjs/microservices and only with a certain argument

throw new RpcException({
   code: 42, // some number code
   message: 'wait. oh sh...', // some string value
});

Also if you using a custom exception filter then it should extends the BaseRpcExceptionFilter from @nestjs/microservices. Call the getType() method on an ArgumentsHost and if it's result equals to 'rpc' then using the super.catch method.

muturgan
  • 473
  • 1
  • 8
  • 19