使用 rabbitMq 处理 nest jest 微服务中的异常

问题描述 投票:0回答:0

这个微服务有问题,

如果我在

users Service
中抛出一些异常,它必须返回到网关然后到
client

但事实并非如此!和 client 只看到默认

500
服务器错误

我在客户端用户服务中都使用了RPC异常过滤器

rpc-exception.filter.ts

import { Catch, RpcExceptionFilter, ArgumentsHost } from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { RpcException } from '@nestjs/microservices';

@Catch(RpcException)
export class MicroserviceExceptionFilter implements RpcExceptionFilter<RpcException> {
    catch(exception: RpcException, host: ArgumentsHost): Observable<any> {
        return throwError(() => exception.getError());
    }
}

这是网关main.ts

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const environmentService = app.get(EnvironmentService);
  app.useGlobalFilters(new MicroserviceExceptionFilter());
  await app.listen(environmentService.getAppPort());
}
bootstrap();

那么我如何向用户显示某些服务中发生的exception

typescript exception nestjs microservices
© www.soinside.com 2019 - 2024. All rights reserved.