【问题标题】:NestJS-Microservice-TCP Exception Error HandlingNestJS-微服务-TCP异常错误处理
【发布时间】:2022-06-15 19:50:15
【问题描述】:

我正在测试 NestJs,特别是微服务部分,我发现每次客户端向失败/断开连接的微服务 (TCP) 发送消息时,都会引发一个错误,它似乎无法在客户端上捕获(至少,我已经尝试过使用 try / catch 方法)this.client.send(...)。

users.services.ts

来自网关(发布者)。

async registerUser(user: RegisterUserDto) {
  const res = await new Promise<RegisterUserDto>((resolve, reject) => {
    this.usersServiceClient
      .send<RegisterUserDto>({ cmd: 'user_registered' }, user)
      .subscribe({
        next: (data) => resolve(data),
        error: (err) => reject(err),
        complete: () => resolve(null),
      });
  });
  return res;
}

auth.controller.ts

在 Auth Controller 中,使用 @MessagePatter() 函数(监听器)接收事件。

@MessagePattern({ cmd: "user_registered" })
async registerUser(@Body() user: CreateUserDto) {
  return await this.authService.create(user);
}

auth.service.ts

public create = async (user: CreateUserDto) => {
  // hash the password
  const pass = await this.hashPassword(user.password);

  // create the user
  const newUser = await this.userService.create({ ...user, password: pass });

  const { password, ...result } = newUser["dataValues"];

  // generate token
  const token = await this.generateToken(result);

  // return the user and the token
  return { user: result, token };
};

这是我在网关(发布者)中获得的当前错误日志。

[Nest] 3011  - 04/10/2022, 12:29:48 PM     LOG [NestFactory] Starting Nest application...       
[Nest] 3011  - 04/10/2022, 12:29:48 PM     LOG [InstanceLoader] ClientsModule dependencies initialized +21ms
[Nest] 3011  - 04/10/2022, 12:29:48 PM     LOG [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 3011  - 04/10/2022, 12:29:48 PM     LOG [NestMicroservice] Nest microservice successfully started +22ms
[Nest] 3011  - 04/10/2022, 12:29:48 PM     LOG [RoutesResolver] AppController {/}: +26ms        
[Nest] 3011  - 04/10/2022, 12:29:48 PM     LOG [RouterExplorer] Mapped {/, GET} route +2ms      
[Nest] 3011  - 04/10/2022, 12:29:48 PM     LOG [RouterExplorer] Mapped {/, POST} route +1ms     
[Nest] 3011  - 04/10/2022, 12:29:48 PM     LOG [NestApplication] Nest application successfully started +2ms
Gateway API Microservice is listening on 3000
[Nest] 3011  - 04/10/2022, 12:30:15 PM   ERROR [ExceptionsHandler] Internal server error
[Nest] 3011  - 04/10/2022, 12:30:15 PM   ERROR [ExceptionsHandler] undefined

这是用户注册过程中发生任何错误时的响应。

{
    "statusCode": 500,
    "message": "Internal server error"
}

【问题讨论】:

    标签: error-handling microservices nestjs


    【解决方案1】:

    关于这个问题的任何更新???

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 1970-01-01
      • 2022-11-30
      • 2020-06-09
      • 2020-02-04
      • 2018-03-16
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      相关资源
      最近更新 更多