Skip to content

Commit 8657483

Browse files
Jumpei Ogawaphanect
authored andcommitted
Show appropriate error message for model validation error
Before: Possibly unhandled SequelizeValidationError: Validation error at /home/phanect/dev/notel-api/node_modules/sequelize/lib/instance-validator.js:150:14 ... After: Possibly unhandled SequelizeValidationError: field_name cannot be null at /home/phanect/dev/notel-api/node_modules/sequelize/lib/instance-validator.js:150:14 ...
1 parent 81c8b33 commit 8657483

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/errors.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ error.ValidationError = function(message, errors) {
4141
error.BaseError.apply(this, arguments);
4242
this.name = 'SequelizeValidationError';
4343
this.errors = errors || [];
44+
45+
if (this.errors.length > 0 && this.errors[0].message) {
46+
this.message = this.errors.map(function (err) {
47+
return err.type + ": " + err.message;
48+
}).join(',\n');
49+
}
4450
};
4551
util.inherits(error.ValidationError, error.BaseError);
4652

test/integration/error.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var chai = require('chai')
44
, sinon = require('sinon')
55
, expect = chai.expect
6+
, errors = require('../../lib/errors')
67
, Support = require(__dirname + '/support')
78
, Sequelize = Support.Sequelize
89
, Promise = Sequelize.Promise;
@@ -20,7 +21,10 @@ describe(Support.getTestDialectTeaser('Sequelize Errors'), function () {
2021
});
2122
it('Sequelize Errors instances should be instances of Error', function() {
2223
var error = new Sequelize.Error();
23-
var validationError = new Sequelize.ValidationError();
24+
var validationError = new Sequelize.ValidationError('Validation Error', [
25+
new errors.ValidationErrorItem('<field name> cannot be null', 'notNull Violation', '<field name>', null)
26+
, new errors.ValidationErrorItem('<field name> cannot be an array or an object', 'string violation', '<field name>', null)
27+
]);
2428

2529

2630
var sequelize = new Sequelize();
@@ -34,6 +38,7 @@ describe(Support.getTestDialectTeaser('Sequelize Errors'), function () {
3438
expect(validationError).to.be.instanceOf(Sequelize.ValidationError);
3539
expect(validationError).to.be.instanceOf(Error);
3640
expect(validationError).to.have.property('name', 'SequelizeValidationError');
41+
expect(validationError.message).to.match(/notNull Violation: <field name> cannot be null,\nstring violation: <field name> cannot be an array or an object/);
3742

3843
expect(instError).to.be.instanceOf(Sequelize.Error);
3944
expect(instError).to.be.instanceOf(Error);

0 commit comments

Comments
 (0)