不要怂,就是干,撸起袖子干!

Commit 23cb6673 by Mick Hansen

fix(transaction): reject transaction promise when transaction callback does not …

…result in a promise chain
1 parent 550b40bc
...@@ -962,7 +962,10 @@ module.exports = (function() { ...@@ -962,7 +962,10 @@ module.exports = (function() {
deprecated('Note: When passing a callback to a transaction a promise chain is expected in return, the transaction will be committed or rejected based on the promise chain returned to the callback.'); deprecated('Note: When passing a callback to a transaction a promise chain is expected in return, the transaction will be committed or rejected based on the promise chain returned to the callback.');
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
transaction.prepareEnvironment().then(function () { transaction.prepareEnvironment().then(function () {
autoCallback(transaction).then(function (result) { var result = autoCallback(transaction);
if (!result) return reject(new Error('You need to return a promise chain to the sequelize.transaction() callback'));
result.then(function (result) {
return transaction.commit().then(function () { return transaction.commit().then(function () {
resolve(result); resolve(result);
}); });
......
...@@ -5,7 +5,11 @@ var fs = require('fs') ...@@ -5,7 +5,11 @@ var fs = require('fs')
, _ = require('lodash') , _ = require('lodash')
, Sequelize = require(__dirname + "/../index") , Sequelize = require(__dirname + "/../index")
, DataTypes = require(__dirname + "/../lib/data-types") , DataTypes = require(__dirname + "/../lib/data-types")
, Config = require(__dirname + "/config/config"); , Config = require(__dirname + "/config/config")
, chai = require("chai")
, chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
// Make sure errors get thrown when testing // Make sure errors get thrown when testing
Sequelize.Promise.onPossiblyUnhandledRejection(function(e, promise) { Sequelize.Promise.onPossiblyUnhandledRejection(function(e, promise) {
......
...@@ -42,18 +42,19 @@ describe(Support.getTestDialectTeaser("Transaction"), function () { ...@@ -42,18 +42,19 @@ describe(Support.getTestDialectTeaser("Transaction"), function () {
}); });
}); });
it('supports automatically rolling back with a thrown error', function () { it('supports automatically rolling back with a thrown error', function () {
return this.sequelize.transaction(function (t) { return expect(this.sequelize.transaction(function (t) {
throw new Error('Yolo'); throw new Error('Yolo');
}).catch(function (err) { })).to.eventually.be.rejected;
expect(err).to.be.ok;
});
}); });
it('supports automatically rolling back with a rejection', function () { it('supports automatically rolling back with a rejection', function () {
return this.sequelize.transaction(function (t) { return expect(this.sequelize.transaction(function (t) {
return Promise.reject('Swag'); return Promise.reject('Swag');
}).catch(function (err) { })).to.eventually.be.rejected;
expect(err).to.be.ok; });
}); it('errors when no promise chain is returned', function () {
return expect(this.sequelize.transaction(function (t) {
})).to.eventually.be.rejected;
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!