【问题标题】:Throwing HTTP nest Exception results in Promise rejection抛出 HTTP 嵌套异常会导致 Promise 被拒绝
【发布时间】:2019-07-11 23:30:12
【问题描述】:

我正在尝试抛出一个 http 嵌套异常以使用默认嵌套处理程序。

我尝试在调用 adminService.update 函数的控制器中抛出异常,并且非常成功。

  async update(update: DeepPartial<Admin>) {
    const admin = await this.findOne({ id: update.id});
    const adminName = await this.findOne({ username: update.username});
    if (!adminName) {
      throw new ConflictException('Username already in use');
    }
    admin.username = update.username;
    admin.save();
  }

将调用放入控制器时的输出:

{
  "statusCode": 409,
  "error": "Conflict",
  "message": "Username already in use"
}

控制器方法。

  @Put()
  async update(@Body() updateDTO: UpdateDTO): Promise<void> {
    throw new ConflictException('Username already in use');
    this.adminService.update(updateDTO);
  }

错误本身:

UnhandledPromiseRejectionWarning: Error: [object Object] at AdminService.&lt;anonymous&gt; (C:\Users\JBRETAS_EXT\Documents\mapa-digital\dist\admin\admin.service.js:55:19) at Generator.next (&lt;anonymous&gt;)

【问题讨论】:

    标签: node.js nestjs


    【解决方案1】:

    我的控制器方法中似乎缺少 return 语句。

      @Put()
      async update(@Body() updateDTO: UpdateDTO): Promise<void> {
        return this.adminService.update(updateDTO);
      }
    

    【讨论】:

      猜你喜欢
      • 2015-10-11
      • 1970-01-01
      • 2012-09-11
      • 2016-01-06
      • 2012-10-19
      • 1970-01-01
      • 2011-08-09
      • 2018-04-12
      • 2013-05-24
      相关资源
      最近更新 更多