// ----------------------------------------------------------------------------
//
// myddle.js - Context carrying Middleware for regular functions, with error handlers.
//
// Copyright 2013 Andrew Chilton. All Rights Reserved.
//
// License: MIT - http://chilts.mit-license.org/2013/
//
// ----------------------------------------------------------------------------

module.exports = function(context, fns, callback) {
    console.log('1 - ' + typeof context);
    console.log('1 - ' + Array.isArray(context));
    console.log('2 - ' + typeof fns);
    console.log('3 - ' + typeof callback);

    // if context isn't given
    if ( Array.isArray(context) ) {
        callback = fns;
        fns = context;
        context = {};
    }

    if ( typeof callback !== 'function' ) {
        callback = context;
        context = {};
    }
    console.log('1 - ' + typeof context);
    console.log('1 - ' + Array.isArray(context));
    console.log('2 - ' + typeof fns);
    console.log('3 - ' + typeof callback);

    fns.forEach(function(fn) {
        console.log(fn);
        console.log(fn.length);
    });

    // call each function (of length 1 or 2) in turn
    var upToIndex = -1;
    var err = null;

    function doNext() {
        console.log('doNext(): entry');

        // incremement to the next function
        upToIndex = upToIndex + 1;

        // we're finished since there are no more fns
        if ( upToIndex === fns.length ) {
            console.log('doNextError(): calling callback');
            return callback(err, context);
        }

        console.log('- fns:', fns);
        console.log('- upToIndex:', upToIndex);
        console.log('- length1:', fns[upToIndex].length);
        console.log('- length2:', fns.length);

        // if this is an error function, just skip to the next one
        // since we're not in an error state
        if ( fns[upToIndex].length > 2 ) {
            return process.nextTick(function() {
                doNext();
            });
        }

        console.log('upToIndex:', upToIndex);

        // call this function with the context
        console.log('doNext(): calling next myddleware at ' + upToIndex);
        fns[upToIndex](context, function(thisErr) {
            if (thisErr) {
                console.log('doNext(): got an error after calling the regular myddleware');
                err = thisErr;
                return doNextError();
            }

            console.log('doNext(): no error after calling the regular myddleware');

            // no error, so continue back on the regular chain and clear the error
            err = null;
            doNext();
        });
    }

    function doNextError() {
        console.log('doNextError(): entry');

        // incremement to the next function
        upToIndex = upToIndex + 1;

        // we're finished since there are no more fns
        if ( upToIndex === fns.length ) {
            console.log('doNextError(): calling callback');
            return callback(err, context);
        }

        console.log('- fns:', fns);
        console.log('- upToIndex:', upToIndex);
        console.log('- length1:', fns[upToIndex].length);
        console.log('- length2:', fns.length);

        // if this is a normal function, just skip to the next one
        // since we're currently in an error state
        if ( fns[upToIndex].length < 3 ) {
            return process.nextTick(function() {
                doNextError();
            });
        }

        console.log('upToIndex:', upToIndex);

        // call this function with the err and the context
        console.log('doNextError(): calling next myddleware at ' + upToIndex);
        fns[upToIndex](err, context, function(thisErr) {
            if (thisErr) {
                console.log('doNextError(): got an error after calling the error myddleware');
                err = thisErr;
                return doNextError();
            }
            // no error, so continue back on the regular chain and clear the error
            console.log('doNextError(): No error after calling the error myddleware');
            err = null;
            doNext();
        });
    }

    // kick things off
    doNext();
};

// ----------------------------------------------------------------------------

@@ -418,182 +418,8 @@
 ) %7B%0A
-    console.log('1 - ' + typeof context);%0A    console.log('1 - ' + Array.isArray(context));%0A    console.log('2 - ' + typeof fns);%0A    console.log('3 - ' + typeof callback);%0A%0A
     
@@ -659,278 +659,8 @@
    %7D
-%0A    console.log('1 - ' + typeof context);%0A    console.log('1 - ' + Array.isArray(context));%0A    console.log('2 - ' + typeof fns);%0A    console.log('3 - ' + typeof callback);%0A%0A    fns.forEach(function(fn) %7B%0A        console.log(fn);%0A        console.log(fn.length);%0A    %7D);
 %0A%0A  
@@ -783,49 +783,8 @@
 ) %7B%0A
-        console.log('doNext(): entry');%0A%0A
     
@@ -971,1063 +971,515 @@
     
-console.log('doNextError(): calling callback');%0A            return callback(err, context);%0A        %7D%0A%0A        console.log('- fns:', fns);%0A        console.log('- upToIndex:', upToIndex);%0A        console.log('- length1:', fns%5BupToIndex%5D.length);%0A        console.log('- length2:', fns.length);%0A%0A        // if this is an error function, just skip to the next one%0A        // since we're not in an error state%0A        if ( fns%5BupToIndex%5D.length %3E 2 ) %7B%0A            return process.nextTick(function() %7B%0A                doNext();%0A            %7D);%0A        %7D%0A%0A        console.log('upToIndex:', upToIndex);%0A%0A        // call this function with the context%0A        console.log('doNext(): calling next myddleware at ' + upToIndex);%0A        fns%5BupToIndex%5D(context, function(thisErr) %7B%0A            if (thisErr) %7B%0A                console.log('doNext(): got an error after calling the regular myddleware');%0A                err = thisErr;%0A                return doNextError();%0A            %7D%0A%0A            console.log('doNext(): no error after calling the regular myddleware');
+return callback(err, context);%0A        %7D%0A%0A        // if this is an error function, just skip to the next one%0A        // since we're not in an error state%0A        if ( fns%5BupToIndex%5D.length %3E 2 ) %7B%0A            return process.nextTick(function() %7B%0A                doNext();%0A            %7D);%0A        %7D%0A%0A        // call this function with the context%0A        fns%5BupToIndex%5D(context, function(thisErr) %7B%0A            if (thisErr) %7B%0A                err = thisErr;%0A                return doNextError();%0A            %7D
 %0A%0A  
@@ -1657,54 +1657,8 @@
 ) %7B%0A
-        console.log('doNextError(): entry');%0A%0A
     
@@ -1845,298 +1845,48 @@
     
-console.log('doNextError(): calling callback');%0A            return callback(err, context);%0A        %7D%0A%0A        console.log('- fns:', fns);%0A        console.log('- upToIndex:', upToIndex);%0A        console.log('- length1:', fns%5BupToIndex%5D.length);%0A        console.log('- length2:', fns.length);
+return callback(err, context);%0A        %7D
 %0A%0A  
@@ -2155,55 +2155,8 @@
  %7D%0A%0A
-        console.log('upToIndex:', upToIndex);%0A%0A
     
@@ -2214,87 +2214,8 @@
 ext%0A
-        console.log('doNextError(): calling next myddleware at ' + upToIndex);%0A
     
@@ -2298,103 +2298,8 @@
 ) %7B%0A
-                console.log('doNextError(): got an error after calling the error myddleware');%0A
     
@@ -2464,95 +2464,8 @@
 ror%0A
-            console.log('doNextError(): No error after calling the error myddleware');%0A
     

Remove all debugging