Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rudfoss opened this issue Aug 27, 2018 · 1 comment
Open

Comments

@rudfoss
Copy link

rudfoss commented Aug 27, 2018

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 rudfoss changed the title inversify-express-middleware returning this.statusCode(401) results in inversify-express-middleware throws when returning this.statusCode(401) Aug 27, 2018
@Halynsky
Copy link

Can you show original ts code?

@notaphplover notaphplover transferred this issue from inversify/InversifyJS Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

2 participants