routing-controllers
routing-controllers copied to clipboard
fix: socket hang up when CurrentUser lookup fails
Description
I think this is related to https://github.com/typestack/routing-controllers/issues/243. If currentUserChecker doesn't return a user, we try to send headers after the socket is destroyed, which causes issues. Adding the middleware here fixes the problem but breaks routing-controller's logging.
Check here for example code. Running jest
in that repo will output:
User Controller Tests
GET /users
✕ should throw an error if the user cannot be found (44 ms)
● User Controller Tests › GET /users › should throw an error if the user cannot be found
socket hang up
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 2.46 s, estimated 3 s
Expected behavior
currentUserChecker should be able to return null / false
Actual behavior
if currentUserChecker returns false, headers are sent after the socket is terminated, and express gets unhappy
I'm also getting this when class-validation fails
This issue doesn't appear when default error handler is false