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
Conversation
Not sure why Travis is failing, locally all tests are passing.. 689 tests complete (4922 ms) Coverage: 100.00% |
use .isBoom instead of instanceof Boom
I'm not sure checking |
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 |
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. |
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? |
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. |
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();
}
}; |
confirmed that works fine. sounds like this should be reverted and replaced with some sample code instead. thanks guys. |
Thanks for your help, I will add a better example and documentation around this issue. |
use .isBoom instead of instanceof Boom
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.