inversify-express-utils icon indicating copy to clipboard operation
inversify-express-utils copied to clipboard

inversify-express-middleware throws when returning this.statusCode(401)

Open rudfoss opened this issue 7 years ago • 1 comments

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)

  1. Set up a basic project and create a controller
  2. Create async handler for an httpGet path
  3. 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. (C:\Users\Thomas Rudfoss\Projects\demo-node-server\node_modules\inversify-express-utils\lib\server.js:201:60) at step (C:\Users\Thomas Rudfoss\Projects\demo-node-server\node_modules\inversify-express-utils\lib\server.js:32:23) at Object.next (C:\Users\Thomas Rudfoss\Projects\demo-node-server\node_modules\inversify-express-utils\lib\server.js:13:53) at C:\Users\Thomas Rudfoss\Projects\demo-node-server\node_modules\inversify-express-utils\lib\server.js:7:71 at new Promise () at __awaiter (C:\Users\Thomas Rudfoss\Projects\demo-node-server\node_modules\inversify-express-utils\lib\server.js:3:12) at InversifyExpressServer.handleHttpResponseMessage (C:\Users\Thomas Rudfoss\Projects\demo-node-server\node_modules\inversify-express-utils\lib\server.js:195:16) at InversifyExpressServer. (C:\Users\Thomas Rudfoss\Projects\demo-node-server\node_modules\inversify-express-utils\lib\server.js:242:51) at step (C:\Users\Thomas Rudfoss\Projects\demo-node-server\node_modules\inversify-express-utils\lib\server.js:32:23) at Object.next (C:\Users\Thomas Rudfoss\Projects\demo-node-server\node_modules\inversify-express-utils\lib\server.js:13:53)

rudfoss avatar Aug 27 '18 12:08 rudfoss

Can you show original ts code?

Halynsky avatar Aug 31 '18 08:08 Halynsky