Skip to content

Commit

Permalink
proof of concept for error handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed May 10, 2016
1 parent d378113 commit e4a07d9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 28 deletions.
83 changes: 56 additions & 27 deletions index.js
Expand Up @@ -117,16 +117,17 @@ Kareem.prototype.execPost = function(name, context, args, options, callback) {
var numPosts = posts.length;
var currentPost = 0;

var firstError = null;
if (options && options.error) {
firstError = options.error;
}

if (!numPosts) {
return process.nextTick(function() {
callback.apply(null, [null].concat(args));
callback.apply(null, [firstError].concat(args));
});
}

var firstError = null;
if (options && options.error) {
firstError = options.error;
}
var next = function() {
var post = posts[currentPost];

Expand Down Expand Up @@ -185,53 +186,81 @@ Kareem.prototype.execPostSync = function(name, context) {
}
};

Kareem.prototype.wrap = function(name, fn, context, args, useLegacyPost) {
Kareem.prototype.wrap = function(name, fn, context, args, options) {
var lastArg = (args.length > 0 ? args[args.length - 1] : null);
var argsWithoutCb = typeof lastArg === 'function' ?
args.slice(1) :
args;
var _this = this;

var useLegacyPost;
if (typeof options === 'object') {
useLegacyPost = options && options.useLegacyPost;
} else {
useLegacyPost = options;
}
options = options || {};

this.execPre(name, context, function(error) {
if (error) {
if (typeof lastArg === 'function') {
return lastArg(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;
}

var end = (typeof lastArg === 'function' ? args.length - 1 : args.length);

fn.apply(context, args.slice(0, end).concat(function() {
var argsWithoutError = Array.prototype.slice.call(arguments, 1);
if (arguments[0]) {
// Assume error
return typeof lastArg === 'function' ?
lastArg(arguments[0]) :
undefined;
}

if (useLegacyPost && typeof lastArg === 'function') {
lastArg.apply(context, arguments);
}

var argsWithoutError = Array.prototype.slice.call(arguments, 1);
_this.execPost(name, context, argsWithoutError, function() {
if (arguments[0]) {
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;
}
} else {
if (useLegacyPost && typeof lastArg === 'function') {
lastArg.apply(context, arguments);
}

return typeof lastArg === 'function' && !useLegacyPost ?
lastArg.apply(context, arguments) :
undefined;
});
_this.execPost(name, context, argsWithoutError, function() {
if (arguments[0]) {
return typeof lastArg === 'function' ?
lastArg(arguments[0]) :
undefined;
}

return typeof lastArg === 'function' && !useLegacyPost ?
lastArg.apply(context, arguments) :
undefined;
});
}
}));
});
};

Kareem.prototype.createWrapper = function(name, fn, context) {
Kareem.prototype.createWrapper = function(name, fn, context, options) {
var _this = this;
return function() {
var args = Array.prototype.slice.call(arguments);
_this.wrap(name, fn, context, args);
_this.wrap(name, fn, context, args, options);
};
};

Expand Down
28 changes: 27 additions & 1 deletion test/wrap.test.js
Expand Up @@ -125,6 +125,32 @@ describe('wrap()', function() {
args);
});

it('defers errors to post hooks if enabled', function(done) {
hooks.pre('cook', function(done) {
done(new Error('fail'));
});

hooks.post('cook', function(error, callback) {
callback(new Error('another error occurred'));
});

var args = [];
args.push(function(error) {
assert.equal(error.message, 'another error occurred');
done();
});

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 Expand Up @@ -274,4 +300,4 @@ describe('wrap()', function() {
args,
true);
});
});
});

0 comments on commit e4a07d9

Please sign in to comment.