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

Commit 15f6ad96 by Nicholas Drane Committed by Jan Aagaard Meier

Throw an error when attribute options are not formatted correctly (#7162)

* Throw an error when attribute options are not formatted correctly

* remove space

* update changelong
1 parent 789d881d
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
- [FIXED] Information warnings when findAll is given incorrect inputs [#7047](https://github.com/sequelize/sequelize/pull/7047) - [FIXED] Information warnings when findAll is given incorrect inputs [#7047](https://github.com/sequelize/sequelize/pull/7047)
- [FIXED] scope method syntax loses parameters when used multiple times [#7058](https://github.com/sequelize/sequelize/issues/7058) - [FIXED] scope method syntax loses parameters when used multiple times [#7058](https://github.com/sequelize/sequelize/issues/7058)
- [INTERNAL] Updated to `generic-pool@3.1.6` [#7109](https://github.com/sequelize/sequelize/issues/7109) - [INTERNAL] Updated to `generic-pool@3.1.6` [#7109](https://github.com/sequelize/sequelize/issues/7109)
- [FIXED] findAll throws error if attributes option is formatted incorrectly [#7162](https://github.com/sequelize/sequelize/issues/7163)
## BC breaks: ## BC breaks:
- `DATEONLY` now returns string in `YYYY-MM-DD` format rather than `Date` type - `DATEONLY` now returns string in `YYYY-MM-DD` format rather than `Date` type
......
...@@ -383,3 +383,16 @@ class AssociationError extends BaseError { ...@@ -383,3 +383,16 @@ class AssociationError extends BaseError {
} }
} }
exports.AssociationError = AssociationError; exports.AssociationError = AssociationError;
/**
* Thrown when a query is passed invalid options (see message for details)
* @extends BaseError
* @memberof Errors
*/
class QueryError extends BaseError {
constructor(message) {
super(message);
this.name = 'SequelizeQueryError';
this.message = message;
}
}
exports.QueryError = QueryError;
...@@ -1451,7 +1451,13 @@ class Model { ...@@ -1451,7 +1451,13 @@ class Model {
*/ */
static findAll(options) { static findAll(options) {
if (options !== undefined && !_.isPlainObject(options)) { if (options !== undefined && !_.isPlainObject(options)) {
throw new Error('The argument passed to findAll must be an options object, use findById if you wish to pass a single primary key value'); throw new sequelizeErrors.QueryError('The argument passed to findAll must be an options object, use findById if you wish to pass a single primary key value');
}
if (options !== undefined && options.attributes) {
if (!Array.isArray(options.attributes) && !_.isPlainObject(options.attributes)) {
throw new sequelizeErrors.QueryError('The attributes option must be an array of column names or an object');
}
} }
this.warnOnInvalidOptions(options, Object.keys(this.rawAttributes)); this.warnOnInvalidOptions(options, Object.keys(this.rawAttributes));
......
...@@ -7,6 +7,7 @@ const current = Support.sequelize; ...@@ -7,6 +7,7 @@ const current = Support.sequelize;
const sinon = require('sinon'); const sinon = require('sinon');
const DataTypes = require(__dirname + '/../../../lib/data-types'); const DataTypes = require(__dirname + '/../../../lib/data-types');
const Utils = require('../../../lib/utils.js'); const Utils = require('../../../lib/utils.js');
const sequelizeErrors = require('../../../lib/errors');
describe(Support.getTestDialectTeaser('Model'), () => { describe(Support.getTestDialectTeaser('Model'), () => {
describe('warnOnInvalidOptions', () => { describe('warnOnInvalidOptions', () => {
...@@ -65,6 +66,11 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -65,6 +66,11 @@ describe(Support.getTestDialectTeaser('Model'), () => {
Model.findAll(); Model.findAll();
expect(this.warnOnInvalidOptionsStub.calledOnce).to.equal(true); expect(this.warnOnInvalidOptionsStub.calledOnce).to.equal(true);
}); });
it('Throws an error when the attributes option is formatted incorrectly', () => {
const errorFunction = Model.findAll.bind(Model, {attributes: 'name'});
expect(errorFunction).to.throw(sequelizeErrors.QueryError);
});
}); });
describe('attributes include / exclude', () => { describe('attributes include / exclude', () => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!