javascript - NestJS:全局错误处理程序未捕获未处理的承诺拒绝
问题描述
我正在使用 NestJS,我有一个控制器,它从抛出异常的服务中调用异步方法,如下所示:
@Get('gde/expedientes/:expediente/caratula')
async readExpedientesCaratulaFromGDE(@Param('expediente') expediente: string): Promise<readCaratulaResultType> {
return await this.client.readExpedienteCaratula(expediente) // this throws an Exception!
}
该服务只是抛出常规 JavaScript 错误,而不是 NestJs HttpExceptions。
我希望全局处理程序能够捕获它,但 http 客户端只是挂起,我在服务器中收到以下错误:
problem with request: getaddrinfo ENOTFOUND xxx.xxx.xxx
(node:17518) UnhandledPromiseRejectionWarning: TypeError: exception.getStatus is not a function
at GDEExceptionFilter.catch (/home/sas/devel/apps/trabajo-it/coordinacion/gestion-documental/tramites-consulta/src/api/dist/gde/gde-exception.filter.js:16:34)
at ExceptionsHandler.invokeCustomFilters (/home/sas/devel/apps/trabajo-it/coordinacion/gestion-documental/tramites-consulta/src/api/node_modules/@nestjs/core/exceptions/exceptions-handler.js:33:26)
at ExceptionsHandler.next (/home/sas/devel/apps/trabajo-it/coordinacion/gestion-documental/tramites-consulta/src/api/node_modules/@nestjs/core/exceptions/exceptions-handler.js:13:18)
at /home/sas/devel/apps/trabajo-it/coordinacion/gestion-documental/tramites-consulta/src/api/node_modules/@nestjs/core/router/router-proxy.js:13:35
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:17518) 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(). (rejection id: 1)
(node:17518) [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.
我确实是throwing inside of an async function without a catch block
,但我期待它由 Nest 处理。我想开发自己的全局处理程序并捕获“请求问题:getaddrinfo ENOTFOUND xxx.xxx.xxx”错误。
有什么办法可以捕捉到它,还是我应该总是将我的控制器方法包含在一个try / catch
块中???
解决方案
推荐阅读
- google-apps-script - 谷歌脚本,脚本后的活动单元格
- mysql - 从 CSV 文件加载数据
- javascript - 如何将我的阵列保存到具有特定编号的本地存储中?
- c# - 如何替换动态 JSON 中的属性值?
- r - 在第一页创建自定义页眉后,从 Markdown pdf 的第二页删除页眉空间
- java - Eclipse 2020-12 - 我无法创建或编辑 groovy 类(在 2020-09 年还可以)
- r - 如何在 R 数据框中获取独占计数
- c++ - Qt/C++ 获取 Json 请求
- json - webpack-dev-server (devServer) 没有收到来自 axios 的 json 数据 (payload) | req.query & req.params 为空
- javascript - 执行时编写 Google Apps 脚本