NestJS: global error handler not catching Unhandled promise rejection

Viewed 1065

I'm working with NestJS and I have a controller which calls an async method from a service that throws and exception, like this:

@Get('gde/expedientes/:expediente/caratula')
  async readExpedientesCaratulaFromGDE(@Param('expediente') expediente: string): Promise<readCaratulaResultType> {
    return await this.client.readExpedienteCaratula(expediente)  // this throws an Exception!
  }

The service just throws regular JavaScript Errors, not NestJs HttpExceptions.

I was expecting the global handler to catch it, but instead the http client just hangs and I get the following error in the server:

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.

I am indeed throwing inside of an async function without a catch block but I was expecting it to be handled by Nest. I'd like to develop my own global handler and capture the "problem with request: getaddrinfo ENOTFOUND xxx.xxx.xxx" error.

Is there some way to catch it, or should I always enclose my controller methods in a try / catch block???

0 Answers
Related