From 7ba00683715457fa26575c34bf34168900c055d5 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Sat, 12 May 2018 19:02:36 +0200 Subject: [PATCH 1/2] feat(error): add modified paths to VersionError backport #6464 to 4.x --- lib/error/version.js | 6 ++++-- lib/model.js | 5 ++++- test/versioning.test.js | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/error/version.js b/lib/error/version.js index 33d1bd88e66..8386aefb474 100644 --- a/lib/error/version.js +++ b/lib/error/version.js @@ -13,11 +13,13 @@ var MongooseError = require('./'); * @api private */ -function VersionError(doc, currentVersion) { +function VersionError(doc, currentVersion, modifiedPaths) { + var modifiedPathsStr = modifiedPaths.join(', '); MongooseError.call(this, 'No matching document found for id "' + doc._id + - '" version ' + currentVersion); + '" version ' + currentVersion + 'modifiedPaths "' + modifiedPathsStr + '"'); this.name = 'VersionError'; this.version = currentVersion; + this.modifiedPaths = modifiedPaths; } /*! diff --git a/lib/model.js b/lib/model.js index e502bc8479f..f0ba6eb4324 100644 --- a/lib/model.js +++ b/lib/model.js @@ -229,6 +229,9 @@ Model.prototype.$__save = function(options, callback) { }); } + // store the modified paths before the document is reset + var modifiedPaths = _this.modifiedPaths(); + _this.$__reset(); var numAffected = 0; @@ -263,7 +266,7 @@ Model.prototype.$__save = function(options, callback) { if (numAffected <= 0) { // the update failed. pass an error back - var err = new VersionError(_this, version); + var err = new VersionError(_this, version, modifiedPaths); return callback(err); } diff --git a/test/versioning.test.js b/test/versioning.test.js index 4e6856c8b08..4fa34acd7c9 100644 --- a/test/versioning.test.js +++ b/test/versioning.test.js @@ -266,6 +266,7 @@ describe('versioning', function() { assert.ok(/No matching document/.test(err), err); assert.equal(a._doc.__v, 5); assert.equal(err.version, b._doc.__v - 1); + assert.deepEqual(err.modifiedPaths, ['numbers', 'numbers.2']); a.set('arr.0.0', 'updated'); var d = a.$__delta(); assert.equal(a._doc.__v, d[0].__v, 'version should be added to where clause'); From 5046cef4393b3a705ab36ddfdf08569a232aaddd Mon Sep 17 00:00:00 2001 From: Sean Lavine Date: Mon, 20 Aug 2018 11:57:38 -0700 Subject: [PATCH 2/2] fix str spacing --- lib/error/version.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/error/version.js b/lib/error/version.js index 8386aefb474..eed37fd5888 100644 --- a/lib/error/version.js +++ b/lib/error/version.js @@ -16,7 +16,7 @@ var MongooseError = require('./'); function VersionError(doc, currentVersion, modifiedPaths) { var modifiedPathsStr = modifiedPaths.join(', '); MongooseError.call(this, 'No matching document found for id "' + doc._id + - '" version ' + currentVersion + 'modifiedPaths "' + modifiedPathsStr + '"'); + '" version ' + currentVersion + ' modifiedPaths "' + modifiedPathsStr + '"'); this.name = 'VersionError'; this.version = currentVersion; this.modifiedPaths = modifiedPaths;