Skip to content

Commit

Permalink
Merge pull request #6928 from hellodigit/4.x-modifiedpaths-pr
Browse files Browse the repository at this point in the history
feature(error): add modified paths to VersionError (4.x)
  • Loading branch information
vkarpov15 committed Aug 28, 2018
2 parents 631f476 + 5046cef commit a738273
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/error/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*!
Expand Down
5 changes: 4 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions test/versioning.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit a738273

Please sign in to comment.