nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.7k stars 7.63k forks source link

ExceptionFilter: super.catch(exception, host) give Error [ERR_HTTP_HEADERS_SENT] #6540

Closed vahidvdn closed 3 years ago

vahidvdn commented 3 years ago

Bug Report

Current behavior

I have a HttpExceptionFilter class. I have the following logic:

if (status != 500) super.catch(exception, host);

response.status(status).json({
  statusCode: status,
  message: exception.message,
});

If I get any kind of exception other than 500 error, I get the following error in my console:

(node:17392) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client      
    at ServerResponse.setHeader (_http_outgoing.js:558:11)
    at ServerResponse.header (C:\Users\vahidnajafi\felixin\pantryon-backend\node_modules\express\lib\response.js:771:10)
    at ServerResponse.send (C:\Users\vahidnajafi\felixin\pantryon-backend\node_modules\express\lib\response.js:170:12)
    at ServerResponse.json (C:\Users\vahidnajafi\felixin\pantryon-backend\node_modules\express\lib\response.js:267:15)
    at HttpExceptionFilter.catch (C:\Users\vahidnajafi\felixin\pantryon-backend\dist\shared\filter\http-exception.filter.js:21:33)      
    at ExceptionsHandler.invokeCustomFilters (C:\Users\vahidnajafi\felixin\pantryon-backend\node_modules\@nestjs\core\exceptions\exceptions-handler.js:33:26)
    at ExceptionsHandler.next (C:\Users\vahidnajafi\felixin\pantryon-backend\node_modules\@nestjs\core\exceptions\exceptions-handler.js:13:18)
    at C:\Users\vahidnajafi\felixin\pantryon-backend\node_modules\@nestjs\core\router\router-explorer.js:147:33
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:17392) 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:17392) [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.

Expected behavior

Not to get any errors.

Environment


Nest version: 7.x


For Tooling issues:
- Node version: 14.x
- Platform:  Windows             
kamilmysliwiec commented 3 years ago

Please provide a minimum reproduction repository.

vahidvdn commented 3 years ago

@kamilmysliwiec Here you are: https://github.com/vahidvdn/typescript-starter Please run it and open http://localhost:3000/docs to run the API. Then you will see the error in console.

FYI: I guess this error happened after upgrading my node version. Please make sure you are using the latest version (14.x)

kamilmysliwiec commented 3 years ago

Your code is invalid.

Change this:

if (status != 500) super.catch(exception, host);

to this:

if (status != 500) {
   return super.catch(exception, host);
}
vahidvdn commented 3 years ago

@kamilmysliwiec Thank you. Btw, if you don't mind, I can contribute in docs and change it. (https://docs.nestjs.com/exception-filters#inheritance)