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

Commit 48e8f7b0 by Sushant Committed by GitHub

fix(findAndCount): throws unhandled rejection from findAll (#8887)

1 parent 0d344138
...@@ -1895,25 +1895,18 @@ class Model { ...@@ -1895,25 +1895,18 @@ class Model {
} }
const countOptions = Utils.cloneDeep(options); const countOptions = Utils.cloneDeep(options);
if (countOptions.attributes) { if (countOptions.attributes) {
countOptions.attributes = undefined; countOptions.attributes = undefined;
} }
const countQuery = this.count(countOptions); return Promise.all([
const findQuery = this.findAll(options); this.count(countOptions),
this.findAll(options)
return countQuery.then(count => { ]).spread((count, rows) => ({
if (count === 0) { count,
return { rows: count === 0 ? [] : rows
count: 0, }));
rows: []
};
}
return findQuery.then(results => ({
count: count || 0,
rows: results
}));
});
} }
/** /**
......
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require(__dirname + '/../support'),
current = Support.sequelize,
sinon = require('sinon'),
DataTypes = require(__dirname + '/../../../lib/data-types'),
Promise = require('bluebird').getNewLibraryCopy();
describe(Support.getTestDialectTeaser('Model'), () => {
describe('findAndCount', () => {
describe('should handle promise rejection', () => {
before(function() {
this.stub = sinon.stub();
Promise.onPossiblyUnhandledRejection(() => {
this.stub();
});
this.User = current.define('User', {
username: DataTypes.STRING,
age: DataTypes.INTEGER
});
this.findAll = sinon.stub(this.User, 'findAll').callsFake(() => {
return Promise.reject(new Error());
});
this.count = sinon.stub(this.User, 'count').callsFake(() => {
return Promise.reject(new Error());
});
});
after(function() {
this.findAll.resetBehavior();
this.count.resetBehavior();
});
it('with errors in count and findAll both', function() {
return this.User.findAndCount({})
.then(() => {
throw new Error();
})
.catch(() => {
expect(this.stub.callCount).to.eql(0);
});
});
});
});
});
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!