Skip to content

Commit

Permalink
Smarter handling of next calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
wbyoung committed May 29, 2015
1 parent e1a3c14 commit b78c7cf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
14 changes: 5 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
};
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 8 additions & 5 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
Expand All @@ -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);
});
Expand Down

0 comments on commit b78c7cf

Please sign in to comment.