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

onPreResponse not called when error returned/rethrown from failAction of error resp validation #3821

Closed
dwelle opened this Issue Sep 7, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@dwelle
Contributor

dwelle commented Sep 7, 2018

Is this a security related issue?

no

What are you trying to achieve or the steps to reproduce?

Throwing/returning error from failAction handler of response validation (at (2)) skips onPreResponse ext point.

When failAction is not specified at all, onPreResponse is also skipped.

I Assume that's a bug since nothing like that is noted in docs, quite the contrary:

onPreResponse

  • always called, unless the request is aborted.

In the code below I'm also checking if the request wasn't aborted which is the only case where onPreResponse shouldn't be called.

(async function () {
    const Hapi = require(`hapi`);
    const Boom = require(`boom`);
    try {
        const server = Hapi.server({
            host: `localhost`,
            port: 3000,
            routes: { cors: true }
        });

        server.route([{
            path: `/test`,
            method: `GET`,
            options: {
                response: {
                    status: {
                        500 () {
                            console.log(`(1)`);
                            throw new Error(`error didn't pass validation`);
                        },
                    },
                    failAction () {
                        console.log(`(2)`);
                        // throw notFound to see if server correctly responds
                        //  with different error than the originally thrown
                        throw Boom.notFound();
                    }
                }
            },
            handler () {

                throw new Boom(`testing`);
            }
        }]);

        server.ext(`onRequest`, (request, h) => {
            request.events.once(`disconnect`, () => {
                console.error(`request aborted`); // not called
            });
            return h.continue;
        });

        server.ext(`onPreResponse`, (request, h) => {
            console.log(`(3)`); // not called
            return h.continue;
        });        
        
        server.events.on(`request`, (request, event) => {
            console.log(event.tags.includes(`abort`)); // false
        });

        await server.start();
        console.log(`Server started on ${server.info.uri}`);
    } catch ( err ) {
        console.error(err);
    }
})();

The above is throwing an error, but also happens when returning an error:

failAction ( request, h, error ) {
    
    return error;
}
fetch(`http://localhost:3000/test`)

What was the result you received?

onPreResponse ext not called.

What did you expect?

onPreResponse to be called.

Context

  • node version: 10.0.0
  • hapi version: 17.5.4
  • os: Windows 7

EDIT: in fact happens even if error simply returned, not only when error is thrown.
EDIT2: happens also if failAction not specified

@dwelle dwelle changed the title from onPreResponse not called when error thrown in failAction of error resp validation to onPreResponse not called when error returned/rethrown from failAction of error resp validation Sep 7, 2018

@hueniverse hueniverse self-assigned this Sep 22, 2018

@hueniverse hueniverse added the bug label Sep 22, 2018

@hueniverse hueniverse added this to the 17.5.5 milestone Sep 22, 2018

@SimonSchick

This comment has been minimized.

SimonSchick commented Sep 23, 2018

Oh, I had this issue before, ty for fixing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment