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

Commit 0d0aad6a by Jozef Hartinger

Log which query tried to run on a finished transaction

The "commit has been called on this transaction" error message usually indicates a problem with chaining of query promises within a transaction block. In more complex transaction blocks it may not be immediately clear which query is improperly chained and the current error message does not help much. This change attaches the rejected query (SQL) as the 'sql' property of the error. This makes it easier to identify a problem in an application.
1 parent 5c177d97
...@@ -789,7 +789,9 @@ Sequelize.prototype.query = function(sql, options) { ...@@ -789,7 +789,9 @@ Sequelize.prototype.query = function(sql, options) {
} }
if (options.transaction && options.transaction.finished) { if (options.transaction && options.transaction.finished) {
return Promise.reject(new Error(options.transaction.finished+' has been called on this transaction('+options.transaction.id+'), you can no longer use it')); var error = new Error(options.transaction.finished+' has been called on this transaction('+options.transaction.id+'), you can no longer use it. (The rejected query is attached as the \'sql\' property of this error)');
error.sql = sql;
return Promise.reject(error);
} }
if (this.test.$trackRunningQueries) { if (this.test.$trackRunningQueries) {
......
...@@ -117,15 +117,17 @@ describe(Support.getTestDialectTeaser('Transaction'), function() { ...@@ -117,15 +117,17 @@ describe(Support.getTestDialectTeaser('Transaction'), function() {
it('does not allow queries after commit', function() { it('does not allow queries after commit', function() {
var self = this; var self = this;
return expect( return this.sequelize.transaction().then(function(t) {
this.sequelize.transaction().then(function(t) {
return self.sequelize.query('SELECT 1+1', {transaction: t, raw: true}).then(function() { return self.sequelize.query('SELECT 1+1', {transaction: t, raw: true}).then(function() {
return t.commit(); return t.commit();
}).then(function() { }).then(function() {
return self.sequelize.query('SELECT 1+1', {transaction: t, raw: true}); return self.sequelize.query('SELECT 1+1', {transaction: t, raw: true});
}); });
}) }).throw(new Error('Expected error not thrown'))
).to.eventually.be.rejected; .catch (function (err) {
expect (err.message).to.match(/commit has been called on this transaction\([^)]+\), you can no longer use it\. \(The rejected query is attached as the 'sql' property of this error\)/);
expect (err.sql).to.equal('SELECT 1+1');
});
}); });
it('does not allow queries immediatly after commit call', function() { it('does not allow queries immediatly after commit call', function() {
...@@ -135,7 +137,12 @@ describe(Support.getTestDialectTeaser('Transaction'), function() { ...@@ -135,7 +137,12 @@ describe(Support.getTestDialectTeaser('Transaction'), function() {
return self.sequelize.query('SELECT 1+1', {transaction: t, raw: true}).then(function() { return self.sequelize.query('SELECT 1+1', {transaction: t, raw: true}).then(function() {
return Promise.join( return Promise.join(
expect(t.commit()).to.eventually.be.fulfilled, expect(t.commit()).to.eventually.be.fulfilled,
expect(self.sequelize.query('SELECT 1+1', {transaction: t, raw: true})).to.eventually.be.rejectedWith(/commit has been called on this transaction\([^)]+\), you can no longer use it/) self.sequelize.query('SELECT 1+1', {transaction: t, raw: true})
.throw(new Error('Expected error not thrown'))
.catch (function (err) {
expect (err.message).to.match(/commit has been called on this transaction\([^)]+\), you can no longer use it\. \(The rejected query is attached as the 'sql' property of this error\)/);
expect (err.sql).to.equal('SELECT 1+1');
})
); );
}); });
}) })
...@@ -161,7 +168,12 @@ describe(Support.getTestDialectTeaser('Transaction'), function() { ...@@ -161,7 +168,12 @@ describe(Support.getTestDialectTeaser('Transaction'), function() {
this.sequelize.transaction().then(function(t) { this.sequelize.transaction().then(function(t) {
return Promise.join( return Promise.join(
expect(t.rollback()).to.eventually.be.fulfilled, expect(t.rollback()).to.eventually.be.fulfilled,
expect(self.sequelize.query('SELECT 1+1', {transaction: t, raw: true})).to.eventually.be.rejectedWith(/rollback has been called on this transaction\([^)]+\), you can no longer use it/) self.sequelize.query('SELECT 1+1', {transaction: t, raw: true})
.throw(new Error('Expected error not thrown'))
.catch (function (err) {
expect (err.message).to.match(/rollback has been called on this transaction\([^)]+\), you can no longer use it\. \(The rejected query is attached as the 'sql' property of this error\)/);
expect (err.sql).to.equal('SELECT 1+1');
})
); );
}) })
).to.eventually.be.fulfilled; ).to.eventually.be.fulfilled;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!