0

我正在尝试过滤 RcpException (如果重要,来自 RabbitMQ)

网关.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-异常-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'};
    }
}

测试RMQ.ts:

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

预期输出:

{ status: 500, message: 'RpcFilterError' }

给定输出:

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

为什么过滤器不起作用?

4

0 回答 0