【问题标题】:NestJS crashing on throwing http exceptionNestJS在抛出http异常时崩溃
【发布时间】:2022-09-30 03:39:25
【问题描述】:
main.controller.ts:135
throw new NotFoundException(\'Swap not found\');
^
NotFoundException: Swap not found

我在控制器中有一个非常简单的请求

@Get(\"/details\")
@ApiResponse({
    status: 200,
    description: \'Returns the details\',
})
async getDetails(
    @Query(\'id\') id: string
): Promise<Result> {
    let details = await this.databaseService.find(id)
    if (!details) {
        throw new NotFoundException(\'Swap not found\');
    }
    return {
        details: details,
    }
}

抛出异常后,NestJS 也停止运行了!? 我该如何避免这种情况?我知道异常过滤器,但那是针对自定义异常的,对吧?我使用默认的。

不过,以防万一,我添加了一个全局变量来测试和相同的行为:app.useGlobalFilters(new HttpExceptionFilter());

这是预期的行为吗?

  • 这很奇怪。默认行为是捕获该异常并回复 500 错误。你能提供一些重现这个的minimum repo吗?
  • 我确实在崩溃之前得到了 json( statusCode: 404 )。之后的任何请求都没有到达服务器 ERR_CONNECTION_REFUSED
  • 那 sn-p 看起来不错。你不应该因为这种模式而崩溃。如果您分享一些完整的代码,我会帮助您。你可以用这个开始构建它:gitlab.com/micalevisk/minimum-reproduction--nestjs-v8
  • 哦,那太好了,肯定会节省我一些时间!谢谢!我会用叉子回复你
  • 奇怪,它与我在那个仓库中的代码一起工作。

标签: nestjs


【解决方案1】:

我遇到了同样的问题,我通过在抛出之前等待任务完成来修复它,我在发送响应后抛出了一个错误,所以我只是在我的服务中添加了一个等待它需要的地方。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-08
    • 2023-01-03
    • 2013-07-15
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    相关资源
    最近更新 更多