Skip to content

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

@dwelle

Description

@dwelle

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

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions