Skip to content

Commit

Permalink
refactor out handleWrapError helper
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed May 10, 2016
1 parent e4a07d9 commit b19af38
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
41 changes: 17 additions & 24 deletions index.js
Expand Up @@ -186,6 +186,19 @@ Kareem.prototype.execPostSync = function(name, context) {
}
};

function _handleWrapError(instance, error, name, context, args, options, callback) {
if (options.useErrorHandlers) {
var _options = { error: error };
return instance.execPost(name, context, args, _options, function(error) {
return typeof callback === 'function' && callback(error);
});
} else {
return typeof callback === 'function' ?
callback(error) :
undefined;
}
}

Kareem.prototype.wrap = function(name, fn, context, args, options) {
var lastArg = (args.length > 0 ? args[args.length - 1] : null);
var argsWithoutCb = typeof lastArg === 'function' ?
Expand All @@ -203,18 +216,8 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {

this.execPre(name, context, function(error) {
if (error) {
if (options.useErrorHandlers) {
var _options = { error: error };
return _this.execPost(name, context, argsWithoutCb, _options, function(error) {
return typeof lastArg === 'function' ?
lastArg(error) :
undefined;
});
} else {
return typeof lastArg === 'function' ?
lastArg(error) :
undefined;
}
return _handleWrapError(_this, error, name, context, argsWithoutCb,
options, lastArg)
}

var end = (typeof lastArg === 'function' ? args.length - 1 : args.length);
Expand All @@ -223,18 +226,8 @@ Kareem.prototype.wrap = function(name, fn, context, args, options) {
var argsWithoutError = Array.prototype.slice.call(arguments, 1);
if (arguments[0]) {
// Assume error
if (options.useErrorHandlers) {
var _options = { error: arguments[0] };
_this.execPost(name, context, argsWithoutError, _options, function(error) {
return typeof lastArg === 'function' ?
lastArg(error) :
undefined;
});
} else {
return typeof lastArg === 'function' ?
lastArg(arguments[0]) :
undefined;
}
return _handleWrapError(_this, arguments[0], name, context,
argsWithoutError, options, lastArg);
} else {
if (useLegacyPost && typeof lastArg === 'function') {
lastArg.apply(context, arguments);
Expand Down
23 changes: 23 additions & 0 deletions test/wrap.test.js
Expand Up @@ -151,6 +151,29 @@ describe('wrap()', function() {
{ useErrorHandlers: true });
});

it('error handlers with no callback', function(done) {
hooks.pre('cook', function(done) {
done(new Error('fail'));
});

hooks.post('cook', function(error, callback) {
assert.equal(error.message, 'fail');
done();
});

var args = [];

hooks.wrap(
'cook',
function(callback) {
assert.ok(false);
callback();
},
null,
args,
{ useErrorHandlers: true });
});

it('works with no args', function(done) {
hooks.pre('cook', function(done) {
done();
Expand Down

0 comments on commit b19af38

Please sign in to comment.