【问题标题】:NestJS - UnhandledPromiseRejectionWarningNestJS - UnhandledPromiseRejectionWarning
【发布时间】:2021-08-07 14:37:57
【问题描述】:

短版: 我正在寻找一种方法来使用 nodeJS 提供的“--trace-warnings”标志来运行我的 NestJS 应用程序。有没有办法做到这一点,或者 NestJS 是否提供类似的东西?

加长版:

嗨! NestJS 菜鸟在这里。我正在尝试运行我正在开发的 NestJS 应用程序的开发版本。但是,在启动应用程序时,我收到以下错误。

显然,它在某处遗漏了一个 catcherror!但是,开发版有很多更新,而且这个错误可能出现在任何地方,所以我希望找到一种比检查每个新功能更有效的方法来发现这个错误!在错误消息中,有一些关于在启动应用程序时运行标志的提示 (node --trace-warnings ...)。但是,这些是针对 node 而不是 NestJS。

所以我的问题是;有什么方法可以使用 --trace-warnings 标志运行 NestJS 或其他一些有效的方法来找到我错过了 catcherror 的位置?

提前致谢!

错误:

    (node:72899) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:72899) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:72899) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:72899) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
(node:72899) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

【问题讨论】:

  • 你好像没有开启redis

标签: nestjs


【解决方案1】:

回答您的具体问题:

我正在寻找一种方法来使用 nodeJS 提供的“--trace-warnings”标志来运行我的 NestJS 应用程序。有没有办法做到这一点,或者 NestJS 是否提供类似的东西?

我一直在尝试做同样的事情,而且网上似乎几乎没有关于如何做到这一点的文档。经过反复试验,我发现这似乎可行:

node --trace-warnings -r source-map-support/register --inspect dist/main

请注意,这不会重建您的应用程序(无论是在您第一次启动它时,还是通过观察更改),因此您需要手动执行此操作。调试器通过--inspect 开关启用,如果您不需要调试,请将其删除。

【讨论】:

    【解决方案2】:

    尝试使用像 winston 或 moes 这样的 npm 响应/请求记录器,相信我会得到很多帮助。

    这里是这些用于 Nestjs 的库的链接

    https://github.com/scalio/nest-couchdb

    【讨论】:

      【解决方案3】:

      嗯,6379 是 Redis 的默认端口,Nest 似乎无法连接到它:)

      顺便看看这段: https://docs.nestjs.com/exception-filters#catch-everything

      为了捕获每个未处理的异常(无论 异常类型),将 @Catch() 装饰器的参数列表留空, 例如,@Catch()。

      import {
        ExceptionFilter,
        Catch,
        ArgumentsHost,
        HttpException,
        HttpStatus,
      } from '@nestjs/common';
      
      @Catch()
      export class AllExceptionsFilter implements ExceptionFilter {
        catch(exception: unknown, host: ArgumentsHost) {
          const ctx = host.switchToHttp();
          const response = ctx.getResponse();
          const request = ctx.getRequest();
      
          const status =
            exception instanceof HttpException
              ? exception.getStatus()
              : HttpStatus.INTERNAL_SERVER_ERROR;
      
          response.status(status).json({
            statusCode: status,
            timestamp: new Date().toISOString(),
            path: request.url,
          });
        }
      }
      

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      • 感谢您的提示。已编辑
      猜你喜欢
      • 2018-05-20
      • 2021-09-26
      • 2021-05-31
      • 2020-06-26
      • 2020-06-08
      • 2021-06-14
      • 2020-08-23
      • 1970-01-01
      • 2020-01-02
      相关资源
      最近更新 更多