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

@promster/hapi in version 6.1.0 causes an unhandled promise rejection #721

Closed
selaux opened this issue Sep 24, 2021 · 5 comments
Closed

Comments

@selaux
Copy link

selaux commented Sep 24, 2021

Describe the bug

@promster/hapi in version 6.1.0 causes an unhandled promise rejection when using raw response object to build response in hapi:

TypeError: Cannot read properties of undefined (reading 'content-length')
    at Object.onResponseHandler [as listener] (/home/stefan/tmp/promster-issue/node_modules/@promster/hapi/dist/promster-hapi.cjs.dev.js:162:33)
    at module.exports.internals.Podium.emit (/home/stefan/tmp/promster-issue/node_modules/@hapi/podium/lib/index.js:221:110)
    at Request._finalize (/home/stefan/tmp/promster-issue/node_modules/@hapi/hapi/lib/request.js:514:27)
    at Request._abort (/home/stefan/tmp/promster-issue/node_modules/@hapi/hapi/lib/request.js:492:14)
    at Request._reply (/home/stefan/tmp/promster-issue/node_modules/@hapi/hapi/lib/request.js:446:18)
    at Request._execute (/home/stefan/tmp/promster-issue/node_modules/@hapi/hapi/lib/request.js:282:14)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

This can be a problem for integration with other libraries (e.g. NextJS or Apollo) as those might use the raw response to write their responses.

To Reproduce
Steps to reproduce the behavior: Run the minimal example code from below.

  1. Given I request /does-not-work
  2. Then an unhandled rejection is thrown
  3. That might crash the application (depending how unhandled promise rejections are handled)

Expected behavior
Promster does not include the response in the response size metric, or reads the header from the raw response.

Additional context

Minimal example:

const { createPlugin } = require('@promster/hapi');
const Hapi = require('@hapi/hapi');

process.on('unhandledRejection', (err) => {
    console.log(err);
    process.exit(1);
});

(async () => {
    const server = Hapi.server({
        port: 3000,
        host: 'localhost'
    });
    
    await server.register(createPlugin());

    // Default way to render route response works fine
    server.route({
        method: 'GET',
        path: '/works',
        handler: (request, h) => {
            return 'Hello World!';
        }
    });

    // Using raw reponse causes unhandled promise rejection
    server.route({
        method: 'GET',
        path: '/does-not-work',
        handler: (request, h) => {
            request.raw.res.statusCode = 200;
            request.raw.res.write("Hello World!");
            return h.close;
        }
    });
    
    await server.start();
})();
@tdeekens
Copy link
Owner

Thanks for the issue and sorry for the trouble. I will try a fix to access the headers optionally. I found the documentation on accessing headers with Hapi quite confusing to be honest.

@tdeekens
Copy link
Owner

I am releasing @promster/[email protected] which should fix the issue. If it does please close the issue. Thanks.

@selaux
Copy link
Author

selaux commented Oct 1, 2021

Hi. Thanks for the quick response and fix. The issue doesn't manifest itself anymore for me with the new version. I had a quick look at the fix and have one question about it: Won't the fix lead to empty requests & responses not being included in metrics anymore, because of e.g. request?.headers?.['content-length'] evaluating to 0?

@tdeekens
Copy link
Owner

tdeekens commented Oct 1, 2021

That's a good point. You'd suggest that an empty request or response would default to 0, right? Would you mind spinning up a PR for it? Extra ✋🏼 for a test case :)

@tdeekens
Copy link
Owner

tdeekens commented Oct 4, 2021

Released in a new version to default to 0.

@tdeekens tdeekens closed this as completed Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants