Improve properties of ValidationError

- change `statusCode` from 400 to 422
- nest `context` and `codes` inside `details`
- add `details.messages`
- reword the main error message

Remove the call to Error's constructor from ValidationError constructor,
because it's a no-op - Error's constructor creates a new
instance when called via `.call()`.
This commit is contained in:
Miroslav Bajtos 2013-11-25 17:20:05 +01:00
parent 8acb4053ab
commit b5e0035d73
4 changed files with 14 additions and 8 deletions

View File

@ -4,6 +4,7 @@ exports.ModelBuilder = exports.LDL = require('./lib/model-builder.js').ModelBuil
exports.DataSource = exports.Schema = require('./lib/datasource.js').DataSource; exports.DataSource = exports.Schema = require('./lib/datasource.js').DataSource;
exports.ModelBaseClass = require('./lib/model.js'); exports.ModelBaseClass = require('./lib/model.js');
exports.Connector = require('./lib/connector.js'); exports.Connector = require('./lib/connector.js');
exports.ValidationError = require('./lib/validations.js').ValidationError;
var baseSQL = './lib/sql'; var baseSQL = './lib/sql';

View File

@ -599,12 +599,16 @@ function ValidationError(obj) {
if (!(this instanceof ValidationError)) return new ValidationError(obj); if (!(this instanceof ValidationError)) return new ValidationError(obj);
this.name = 'ValidationError'; this.name = 'ValidationError';
this.message = 'Validation error'; this.message = 'The Model instance is not valid. ' +
this.statusCode = 400; 'See `details` property of the error object for more info.';
this.codes = obj.errors && obj.errors.codes; this.statusCode = 422;
this.context = obj && obj.constructor && obj.constructor.modelName;
this.details = {
context: obj && obj.constructor && obj.constructor.modelName,
codes: obj.errors && obj.errors.codes,
messages: obj.errors
};
Error.call(this);
Error.captureStackTrace(this, this.constructor); Error.captureStackTrace(this, this.constructor);
} }

View File

@ -2,6 +2,7 @@
var should = require('./init.js'); var should = require('./init.js');
var db, Person; var db, Person;
var ValidationError = require('..').ValidationError;
describe('manipulation', function() { describe('manipulation', function() {
@ -173,7 +174,7 @@ describe('manipulation', function() {
p.save({ p.save({
'throws': true 'throws': true
}); });
}).should.throw('Validation error'); }).should.throw(ValidationError);
}); });
}); });

View File

@ -2,7 +2,7 @@
var should = require('./init.js'); var should = require('./init.js');
var j = require('../'), db, User; var j = require('../'), db, User;
var ValidationError = require('../lib/validations.js').ValidationError; var ValidationError = j.ValidationError;
function getValidAttributes() { function getValidAttributes() {
return { return {
@ -98,7 +98,7 @@ describe('validations', function() {
User.validatesPresenceOf('name'); User.validatesPresenceOf('name');
User.create(function(e, u) { User.create(function(e, u) {
should.exist(e); should.exist(e);
e.codes.name.should.eql(['presence']); e.details.codes.name.should.eql(['presence']);
done(); done();
}); });
}); });