From b5e0035d7378b3a9188ac36669b65f5153ec04a4 Mon Sep 17 00:00:00 2001 From: Miroslav Bajtos Date: Mon, 25 Nov 2013 17:20:05 +0100 Subject: [PATCH] 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()`. --- index.js | 1 + lib/validations.js | 14 +++++++++----- test/manipulation.test.js | 3 ++- test/validations.test.js | 4 ++-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 34949bb5..79171c1b 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ exports.ModelBuilder = exports.LDL = require('./lib/model-builder.js').ModelBuil exports.DataSource = exports.Schema = require('./lib/datasource.js').DataSource; exports.ModelBaseClass = require('./lib/model.js'); exports.Connector = require('./lib/connector.js'); +exports.ValidationError = require('./lib/validations.js').ValidationError; var baseSQL = './lib/sql'; diff --git a/lib/validations.js b/lib/validations.js index 19ecb987..397841ec 100644 --- a/lib/validations.js +++ b/lib/validations.js @@ -599,12 +599,16 @@ function ValidationError(obj) { if (!(this instanceof ValidationError)) return new ValidationError(obj); this.name = 'ValidationError'; - this.message = 'Validation error'; - this.statusCode = 400; - this.codes = obj.errors && obj.errors.codes; - this.context = obj && obj.constructor && obj.constructor.modelName; + this.message = 'The Model instance is not valid. ' + + 'See `details` property of the error object for more info.'; + this.statusCode = 422; + + 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); } diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 0e9563ae..295160f6 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -2,6 +2,7 @@ var should = require('./init.js'); var db, Person; +var ValidationError = require('..').ValidationError; describe('manipulation', function() { @@ -173,7 +174,7 @@ describe('manipulation', function() { p.save({ 'throws': true }); - }).should.throw('Validation error'); + }).should.throw(ValidationError); }); }); diff --git a/test/validations.test.js b/test/validations.test.js index b87fa5fc..c54b46ec 100644 --- a/test/validations.test.js +++ b/test/validations.test.js @@ -2,7 +2,7 @@ var should = require('./init.js'); var j = require('../'), db, User; -var ValidationError = require('../lib/validations.js').ValidationError; +var ValidationError = j.ValidationError; function getValidAttributes() { return { @@ -98,7 +98,7 @@ describe('validations', function() { User.validatesPresenceOf('name'); User.create(function(e, u) { should.exist(e); - e.codes.name.should.eql(['presence']); + e.details.codes.name.should.eql(['presence']); done(); }); });