UseFilters NestJS doesn't work when RcpException

646 Views Asked by At

I'm trying to filter RcpException (from RabbitMQ if it's matter)

gateway.ts:

@Get('test-exception')
    @HttpCode(HttpStatus.OK)
    @UseFilters(RpcExceptionFilter)
    async testException() {
        try {
            return await lastValueFrom(this.testRMQ.send('test', {}));
        } catch (error) {
            console.log(error);
        }
    }

rpc-exception-filter.ts:

import { Catch, ExceptionFilter } from "@nestjs/common";
import { RpcException } from "@nestjs/microservices";

@Catch(RpcException)
export class RpcExceptionFilter implements ExceptionFilter {
    catch(exception: RpcException) {
        const error = exception.getError();

        return {staus: 500, error: 'RpcFilterError'};
    }
}

testRMQ.ts:

@MessagePattern('test')
    updateInstrumentList(@Payload() payload: void, @Ctx() context: RmqContext) {
        throw new RpcException('Invalid credentials');
    }

Expected output:

{ status: 500, message: 'RpcFilterError' }

Given output:

{ status: 'error', message: 'Invalid credentials' }

Why filter doesn't work?

0

There are 0 best solutions below