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

Commit 979818a1 by Jan Aagaard Meier

Merge pull request #3229 from phanect/validation-error-message

Show more appropriate error message for model validation error
2 parents 3c2071aa 8657483c
Showing with 12 additions and 1 deletions
...@@ -41,6 +41,12 @@ error.ValidationError = function(message, errors) { ...@@ -41,6 +41,12 @@ error.ValidationError = function(message, errors) {
error.BaseError.apply(this, arguments); error.BaseError.apply(this, arguments);
this.name = 'SequelizeValidationError'; this.name = 'SequelizeValidationError';
this.errors = errors || []; this.errors = errors || [];
if (this.errors.length > 0 && this.errors[0].message) {
this.message = this.errors.map(function (err) {
return err.type + ": " + err.message;
}).join(',\n');
}
}; };
util.inherits(error.ValidationError, error.BaseError); util.inherits(error.ValidationError, error.BaseError);
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
var chai = require('chai') var chai = require('chai')
, sinon = require('sinon') , sinon = require('sinon')
, expect = chai.expect , expect = chai.expect
, errors = require('../../lib/errors')
, Support = require(__dirname + '/support') , Support = require(__dirname + '/support')
, Sequelize = Support.Sequelize , Sequelize = Support.Sequelize
, Promise = Sequelize.Promise; , Promise = Sequelize.Promise;
...@@ -20,7 +21,10 @@ describe(Support.getTestDialectTeaser('Sequelize Errors'), function () { ...@@ -20,7 +21,10 @@ describe(Support.getTestDialectTeaser('Sequelize Errors'), function () {
}); });
it('Sequelize Errors instances should be instances of Error', function() { it('Sequelize Errors instances should be instances of Error', function() {
var error = new Sequelize.Error(); var error = new Sequelize.Error();
var validationError = new Sequelize.ValidationError(); var validationError = new Sequelize.ValidationError('Validation Error', [
new errors.ValidationErrorItem('<field name> cannot be null', 'notNull Violation', '<field name>', null)
, new errors.ValidationErrorItem('<field name> cannot be an array or an object', 'string violation', '<field name>', null)
]);
var sequelize = new Sequelize(); var sequelize = new Sequelize();
...@@ -34,6 +38,7 @@ describe(Support.getTestDialectTeaser('Sequelize Errors'), function () { ...@@ -34,6 +38,7 @@ describe(Support.getTestDialectTeaser('Sequelize Errors'), function () {
expect(validationError).to.be.instanceOf(Sequelize.ValidationError); expect(validationError).to.be.instanceOf(Sequelize.ValidationError);
expect(validationError).to.be.instanceOf(Error); expect(validationError).to.be.instanceOf(Error);
expect(validationError).to.have.property('name', 'SequelizeValidationError'); expect(validationError).to.have.property('name', 'SequelizeValidationError');
expect(validationError.message).to.match(/notNull Violation: <field name> cannot be null,\nstring violation: <field name> cannot be an array or an object/);
expect(instError).to.be.instanceOf(Sequelize.Error); expect(instError).to.be.instanceOf(Sequelize.Error);
expect(instError).to.be.instanceOf(Error); expect(instError).to.be.instanceOf(Error);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!