Skip to content

Commit

Permalink
Better error handling for wrapped functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Ribeiro committed Mar 23, 2015
1 parent 7cd0934 commit 66de50f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
28 changes: 19 additions & 9 deletions lib/saferAsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@
*
* @returns {void}
*/
handleTask: function handleTask (options, value, key, tasks) {
handleTask: function handleTask (options, domain, value, key, tasks) {

var taskArr = libs._.isArray(value) ? value : [value];
var task = internals.createTaskObject(options, taskArr, key);

taskArr[taskArr.length - 1] = internals.wrapTask(task);
taskArr[taskArr.length - 1] = internals.wrapTask(task, domain);

tasks[key] = internals.normaliseTaskArray(taskArr, key, tasks);
},
Expand Down Expand Up @@ -183,7 +183,7 @@
*
* @returns {Function}
*/
wrapTask: function wrapTask (task) {
wrapTask: function wrapTask (task, domain) {

/**
* Function which will replace the original task function. It is responsible for creating another function
Expand All @@ -203,10 +203,13 @@
deps: task.deps
});

var wrappedCB = internals.wrapCB(task, callback);
var wrappedCB = internals.wrapCB(task, callback, domain);

internals.bindCallbackTimeout(task, wrappedCB);

domain.enter();
task.fn(wrappedCB, results);
domain.exit();
};
},

Expand All @@ -218,10 +221,10 @@
*
* @returns {Function}
*/
wrapCB: function wrapCB (task, callback) {
wrapCB: function wrapCB (task, callback, domain) {

var counter = 0;

/**
* Function to be passed to the original task function instead of the original async function
*
Expand All @@ -244,7 +247,10 @@
name: task.name,
deps: task.deps
}, task.startObject, arguments);

domain.enter();
callback.apply(this, arguments);
domain.exit();

task = null;
};
Expand Down Expand Up @@ -359,9 +365,9 @@
* @param tasks
* @param callback
*/
var run = function (options, tasks, finishCB) {
var run = function (options, tasks, finishCB, domain) {

libs._.each(tasks, internals.handleTask.bind(this, options));
libs._.each(tasks, internals.handleTask.bind(this, options, domain));
libs.async.auto(tasks, finishCB);
};

Expand All @@ -378,7 +384,10 @@
if (libs._.isFunction(callback)) {

var args = Array.prototype.slice.call(arguments, 2);

domain.enter();
callback.apply(this, args);
domain.exit();
}

domain.exit();
Expand All @@ -388,7 +397,8 @@
this,
options,
tasks,
finish.bind(this, callback, domain)
finish.bind(this, callback, domain),
domain
));
}
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "safer-async",
"version": "0.0.5",
"version": "0.0.6",
"description": "Provides better error handling and profiling for the async library",
"main": "./lib/saferAsync",
"scripts": {
Expand Down

0 comments on commit 66de50f

Please sign in to comment.