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

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() {
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) {
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 () {
resolve(result);
});
......
......@@ -5,7 +5,11 @@ var fs = require('fs')
, _ = require('lodash')
, Sequelize = require(__dirname + "/../index")
, 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
Sequelize.Promise.onPossiblyUnhandledRejection(function(e, promise) {
......
......@@ -42,18 +42,19 @@ describe(Support.getTestDialectTeaser("Transaction"), 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');
}).catch(function (err) {
expect(err).to.be.ok;
});
})).to.eventually.be.rejected;
});
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');
}).catch(function (err) {
expect(err).to.be.ok;
});
})).to.eventually.be.rejected;
});
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!