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

use .isBoom instead of instanceof Boom #951

Merged
merged 1 commit into from Jul 3, 2013
Merged

use .isBoom instead of instanceof Boom #951

merged 1 commit into from Jul 3, 2013

Conversation

nlf
Copy link
Member

@nlf nlf commented Jul 3, 2013

For some reason, the existing check was causing a custom authentication plugin to not passing along a status code. I was able to trace it back to this check.

Despite using Boom to create the error, and the isBoom property being set to true, instanceof Boom was false.

Changing this check to use .isBoom instead resulted in the expected behavior.

@nlf
Copy link
Member Author

nlf commented Jul 3, 2013

Not sure why Travis is failing, locally all tests are passing..

689 tests complete (4922 ms)

Coverage: 100.00%
Coverage succeeded

geek added a commit that referenced this pull request Jul 3, 2013
use .isBoom instead of instanceof Boom
@geek geek merged commit 1f08f80 into hapijs:master Jul 3, 2013
@hueniverse
Copy link
Contributor

I'm not sure checking isBoom alone is safe. We're basically taking over a key that any response object can no longer use. It's ok for now, but I'd like to understand why it wasn't showing instanceof Boom. For example, was the error created with hapi.Error?

@nlf
Copy link
Member Author

nlf commented Jul 4, 2013

here's the plugin I created, sanitized to the smallest possible code, that was giving me grief.

var Boom = require('boom');

exports.register = function (plugin, options, next) {
    plugin.auth('query_user', {
        implementation: {
            authenticate: function (request, callback) {
                if (!request.query.user) return callback(Boom.unauthorized('user is required'));
                var user = request.query.user;
                delete request.query.user;
                return callback(null, user);
            }
        },
        defaultMode: true
    });
    next();
}

The unauthorized error is what was failing to match as instanceof Boom

@hueniverse
Copy link
Contributor

Can you try using hapi.Error instead of Boom without the hapi change? I'm curious if it's a problem of node loading Boom twice.

@geek
Copy link
Member

geek commented Jul 5, 2013

I tried this with hapi 1.8.2 and then installed boom@0.4.0 and was able to reproduce the original issue. If you use Hapi.error instead of using a Boom module with a different version it does work without the PR. What do you all think of reverting this change to force using hapi.error instead of allowing for a different Boom version?

@nlf
Copy link
Member Author

nlf commented Jul 5, 2013

That makes sense to me. Is hapi.Error easily accessible from within a plugin like the above? Or would I still have to require hapi? It seems like it could be useful to hang it off of the plugin object as well if it's not already there.

@geek
Copy link
Member

geek commented Jul 5, 2013

Absolutely, here is the updated code to access error from the plugin:

var plugin = {
    name: 'query_user',
    version: '0.0.1',
    register: function (plugin, options, next) {
        plugin.auth('query_user', {
            implementation: {
                authenticate: function (request, callback) {
                    if (!request.query.user) return callback(plugin.hapi.error.unauthorized('user is required'));
                    var user = request.query.user;
                    delete request.query.user;
                    return callback(null, user);
                }
            },
            defaultMode: true
        });
        next();
    }
};

@nlf
Copy link
Member Author

nlf commented Jul 5, 2013

confirmed that works fine. sounds like this should be reverted and replaced with some sample code instead. thanks guys.

@geek
Copy link
Member

geek commented Jul 5, 2013

Thanks for your help, I will add a better example and documentation around this issue.

jmonster pushed a commit to jmonster/hapi that referenced this pull request Feb 10, 2014
use .isBoom instead of instanceof Boom
@lock lock bot locked as resolved and limited conversation to collaborators Jan 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants