Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

How to handle pre-handler exceptions #445

Closed
bamoo456 opened this issue Feb 28, 2017 · 13 comments
Closed

How to handle pre-handler exceptions #445

bamoo456 opened this issue Feb 28, 2017 · 13 comments

Comments

@bamoo456
Copy link

Hi,

I'm a novice in Hapi.js, does anyone knows how to handle unexpected exceptions in request lifecycle?

In other frameworks(e.g. Koa.js), that will be a default error handler for unhandled exceptions and return 500 errors back to clients.
Just wondering to know if hapi.js support this kind of mechanism ?

@Marsup
Copy link

Marsup commented Feb 28, 2017

hapi already handles that for you through domains, you shouldn't have to do anything, unless you want to change the 500 reply.

@bamoo456
Copy link
Author

Hi Marsup,

Thanks for the explanation.
After several tests on pre-handler, I got weird results.

  • With Promise code inside pre-handler, an uncaught exception cause server crashed
{
    method: 'GET',
    path: 'test,
    config: {
        pre: [{
            assign: '1st',
            method: function (request, reply) {
                new Promise(function (resolve) {
                    console.log('Start pre-handle');
                    resolve();
                }).then(function () {
                    reply('pre-handler done');
                });
            },
        }, {
            assign: '2nd',
            method: function (request, reply) {
                console.log('Ready to throw error');
                if (reply) {
                    throw new Error('test error');
                }
                return reply('Done 2nd handler');
            },
        }],
        handler: function (request, reply) {
            console.log('Processed done', request.pre);
            reply(request.pre);
        },
    },
}
  • Without Promise inside pre-handler, an exception can be caught and reply with 500 error.
{
    method: 'GET',
    path: 'test',
    config: {
        pre: [{
            assign: '1st',
            method: function (request, reply) {
                reply('Done 1st handler');
            },
        }, {
            assign: '2nd',
            method: function (request, reply) {
                console.log('Ready to throw error');
                if (reply) {
                    throw new Error('test error');
                }
                return reply('Done 2nd handler');
            },
        }],
        handler: function (request, reply) {
            console.log('Processed done', request.pre);
            reply(request.pre);
        },
    },
}

Did I use pre-handler in wrong way?

@johnbrett
Copy link

Are you on the latest version of hapi @bamoo456 ? I remember a similar issue to this came up before, but I thought it had been fixed.

@Marsup
Copy link

Marsup commented Mar 1, 2017

I tried on [email protected], it leaks, and I can't even explain it, but I only spent 5 minutes looking.

@kanongil
Copy link

kanongil commented Mar 1, 2017

This sounds like a bug in the way hapi handles pre-handlers. Calling reply inside a promise loses the domain binding for the second handler. Hapi assumes that all calls to reply() are called from the correct domain.

See nodejs/node-v0.x-archive#8648 for some context on why the domain binding is lost inside promises.

@kanongil
Copy link

kanongil commented Mar 1, 2017

I have proposed a fix for hapi in hapijs/hapi#3448.

@johnbrett
Copy link

johnbrett commented Mar 1, 2017

@Marsup I thought this had been fixed!!

global.Promise = require('bluebird') - everything works as expected.

Maybe we need to look at using bluebird in core again? It seems there's constant issues with native promises and domains, I believe bluebird has much more built in to deal with all these edge cases around promise errors with domains...

Edit: Nice work @kanongil, maybe I jumped the gun a little 😆

@bamoo456
Copy link
Author

bamoo456 commented Mar 2, 2017

Hi @johnbrett

Actually, I'm using bluebird as my global promise library, and I encountered that bug.
The project environment is node 6.9.5 and hapi 16.1.0

@acmoune
Copy link

acmoune commented May 19, 2017

I had a more beginner issue. I was thinking that when you do reply(err) from a pre, then the err object will be assigned to the pre assign variable, and you will have to check on your own from the handler if the pre returned an error or not. I thought I had to throw an error from the pre to stop that.

But returning an error from a pre seems to be a particular case, hapi just stop the request there and reply with the error. I think it is cool.

@anuraggupta816
Copy link

Hi
I am getting 500 error instead of 400 error. I am using pre method in hapi js with promise whenever I error occur pre menthod throw 500 error instead of custom error.

@anuraggupta816
Copy link

@anuraggupta816
Copy link

hapi version 17.8.5

@hueniverse
Copy link

@anuraggupta816 this is not how you ask for help. Open a new issue following the instructions. Make sure to provide all the requested information and to open it in the right place. Randomly posting to an issue and then pinging a bunch of people is rude.

@outmoded outmoded locked and limited conversation to collaborators Jun 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants