From cec9ddaf4a1180b0b9730e5351b285a18d873768 Mon Sep 17 00:00:00 2001 From: Hafez Date: Sun, 13 Oct 2019 14:09:21 +0200 Subject: [PATCH 1/2] Minor refactor to ValidationError --- lib/error/validation.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/error/validation.js b/lib/error/validation.js index 85b733c48b3..fb251bd94f1 100644 --- a/lib/error/validation.js +++ b/lib/error/validation.js @@ -17,20 +17,23 @@ const util = require('util'); function ValidationError(instance) { this.errors = {}; + this.name = 'ValidationError'; this._message = ''; + if (instance && instance.constructor.name === 'model') { this._message = instance.constructor.modelName + ' validation failed'; - MongooseError.call(this, this._message); } else { this._message = 'Validation failed'; - MongooseError.call(this, this._message); } - this.name = 'ValidationError'; + + MongooseError.call(this, this._message); + if (Error.captureStackTrace) { Error.captureStackTrace(this); } else { this.stack = new Error().stack; } + if (instance) { instance.errors = this.errors; } From d9163f561311642e36c79be4d40d396efe3f40af Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 14 Oct 2019 09:58:36 -0400 Subject: [PATCH 2/2] fix: correct order for declaration --- lib/error/validation.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/error/validation.js b/lib/error/validation.js index fb251bd94f1..950513575ed 100644 --- a/lib/error/validation.js +++ b/lib/error/validation.js @@ -17,16 +17,15 @@ const util = require('util'); function ValidationError(instance) { this.errors = {}; - this.name = 'ValidationError'; this._message = ''; + MongooseError.call(this, this._message); if (instance && instance.constructor.name === 'model') { this._message = instance.constructor.modelName + ' validation failed'; } else { this._message = 'Validation failed'; } - - MongooseError.call(this, this._message); + this.name = 'ValidationError'; if (Error.captureStackTrace) { Error.captureStackTrace(this);