AlariCode / nestjs-rmq

A custom library for NestJS microservice. It allows you to use RabbitMQ or AMQP.
https://purpleschool.ru
MIT License
285 stars 38 forks source link

How i supposed to catch errors thrown from handlers? #76

Closed tyrkinn closed 9 months ago

tyrkinn commented 9 months ago

Interceptor catch errors like "Requested service doesn't have RMQRoute with this path" but not RMQ errors thrown from @RMQRoute

My config

  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: (configService: ConfigService) => ({
    serviceName: configService.get('APP_NAME'),
    exchangeName: configService.get('EXCHANGE'),
    prefetchCount: parseInt(configService.get('RMQ_PREFETCH_COUNT')),
    queueName: configService.get('APP_NAME'),
    logMessages: process.env.NODE_ENV === 'development',
    connections: [
      {
        login: configService.get('RMQ_USER'),
        password: configService.get('RMQ_PASSWORD'),
        host: configService.get('RMQ_HOST'),
        port: parseInt(configService.get('RMQ_PORT')),
      },
    ],
    intercepters: [RMQErrorInterceptor],
  })

Interceptor:

@Injectable()
export class RMQErrorInterceptor extends RMQIntercepterClass {
  protected logger = new LoggerWithSentry('ERROR INTERCEPTOR');

  intercept(res: any, msg: Message, error: Error): Promise<any> {
    if (error) {
      this.logger.error(error);
    }

    return super.intercept(res, msg, error);
  }
}

Sample handler:

@RMQRoute('sample.route')
  async handleSample() {
    console.log('HERE');
    throw new RMQError(
      'userInfo.internal_server_error',
      ERROR_TYPE.RMQ,
      HttpStatus.INTERNAL_SERVER_ERROR,
    );
  }

If i post message on this route, I get 'HERE' in console but error not getting logged

tyrkinn commented 9 months ago

Sorry, just figured out. That was problem on my side

lenchq commented 9 months ago

Hello @tyrkinn, just encountered the same problem as you had, could you please describe solution in your case? Thank you.