inversify-express-utils
inversify-express-utils copied to clipboard
inversify-express-middleware throws when returning this.statusCode(401)
Expected Behavior
Returning this.statusCode(401) from controller extending BaseHttpController should return 401 status to client.
Current Behavior
Throws:
TypeError: Cannot read property 'headers' of undefined
Possible Solution
Not entirely sure. The code seems to check whether message.content is defined in server.ts, but the compiled code just calls it directly.
// Raw copy from node_modules/inversify-express-utils/lib/server.js
InversifyExpressServer.prototype.handleHttpResponseMessage = function (message, res) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
this.copyHeadersTo(message.headers, res);
this.copyHeadersTo(message.content.headers, res); // Here is the problem
if (!(message.content !== undefined)) return [3 /*break*/, 2];
_b = (_a = res.status(message.statusCode)).send;
return [4 /*yield*/, message.content.readAsStringAsync()];
case 1:
_b.apply(_a, [_c.sent()]);
return [3 /*break*/, 3];
case 2:
res.sendStatus(message.statusCode);
_c.label = 3;
case 3: return [2 /*return*/];
}
});
});
};
Steps to Reproduce (for bugs)
- Set up a basic project and create a controller
- Create async handler for an httpGet path
- return this.statusCode(401)
Context
I'm trying to return 401 errors when users are not authenticated. Preferably I'd like to also return a message, but just the error code is sufficient for now.
Your Environment
Windows 10x64 Node 10 inversify-express-utils version 6.1.0
Stack trace
TypeError: Cannot read property 'headers' of undefined
at InversifyExpressServer.
Can you show original ts code?