Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(error): add modified paths to VersionError (4.x) #6928

Merged
merged 2 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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