From b78c7cf89616e2ed5bd9ed0f47250b98731e06b6 Mon Sep 17 00:00:00 2001 From: Whitney Young Date: Fri, 29 May 2015 14:32:29 -0700 Subject: [PATCH] Smarter handling of next calls. --- index.js | 14 +++++--------- test/tests.js | 13 ++++++++----- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 9c3c42c..25dd424 100644 --- a/index.js +++ b/index.js @@ -92,19 +92,13 @@ var setupResponse = function(db, req, res, next) { * @param {Function} next * @return {Function} */ -var wrapNext = function(db, req, res, next) { +var transactionNext = function(db, req, res, next) { return function() { var args = _.toArray(arguments); var promise = BPromise.resolve(); - if (!args[0] && res.azul) { - promise = res.azul.commit(); - } - else if ((args[0] instanceof Error) && res.azul) { + if (args.length === 1 && (args[0] instanceof Error)) { promise = res.azul.rollback(); } - else if (args[0] && !(args[0] instanceof Error)) { - throw new Error('Unexpected call to `next` with non-error.'); - } return promise.then(next.apply.bind(next, this, args)).catch(next); }; }; @@ -277,7 +271,9 @@ var route = function(db, fn, options) { // wrap next now & all actions from this point forward should use the // wrapped version so that if a transaction is active, it will be rolled // back. - next = wrapNext(db, req, res, next); + if (req.azul.transaction) { + next = transactionNext(db, req, res, next); + } // form express arguments var expressArgs = _.take(args, expressParams.length); diff --git a/test/tests.js b/test/tests.js index 95ee10d..536baeb 100644 --- a/test/tests.js +++ b/test/tests.js @@ -495,7 +495,7 @@ describe('azul-express', function() { .then(done, done); }); - it('commits if next is called without arguments', function(done) { + it('passes on calls to next without arguments', function(done) { var route = ae.route(function(req, res, next, query) { query; // use all params (jshint) next(); @@ -509,12 +509,13 @@ describe('azul-express', function() { expect(next).to.have.been.calledOnce; expect(next).to.have.been.calledWithExactly(); expect(adapter.clients.length).to.eql(1); - expect(adapter.executed).to.eql(['BEGIN', 'COMMIT']); + // commit would occur on write from future middleware + expect(adapter.executed).to.eql(['BEGIN']); }) .then(done, done); }); - it('fails if next is called with non-error', function(done) { + it('passes on calls to next with non-error', function(done) { var route = ae.route(function(req, res, next, query) { query; // use all params (jshint) next('value'); @@ -525,8 +526,10 @@ describe('azul-express', function() { }) .then(function() { expect(next).to.have.been.calledOnce; - expect(next.getCall(0).args[0]).to.match(/call.*next.*non-error/i); - expect(adapter.executed).to.eql(['BEGIN', 'ROLLBACK']); + expect(next).to.have.been.calledWithExactly('value'); + expect(adapter.clients.length).to.eql(1); + // commit would occur on write from future middleware + expect(adapter.executed).to.eql(['BEGIN']); }) .then(done, done); });