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

Add modified paths to VersionError #6464

Merged
merged 2 commits into from
May 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(error): add modified paths to VersionError
Fix #6433
  • Loading branch information
paglias committed May 12, 2018
commit 5b6eebd66dd2b92e3ebd8d84199d98222ab49948
3 changes: 2 additions & 1 deletion lib/error/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ var MongooseError = require('./');
* @api private
*/

function VersionError(doc, currentVersion) {
function VersionError(doc, currentVersion, modifiedPaths) {
MongooseError.call(this, 'No matching document found for id "' + doc._id +
'" version ' + currentVersion);
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 @@ -241,6 +241,9 @@ Model.prototype.$__save = function(options, callback) {
});
}

// store the modified paths before the document is reset
const modifiedPaths = this.modifiedPaths();

this.$__reset();

let numAffected = 0;
Expand Down Expand Up @@ -269,7 +272,7 @@ Model.prototype.$__save = function(options, callback) {

if (numAffected <= 0) {
// the update failed. pass an error back
let err = new VersionError(this, version);
let err = new VersionError(this, version, modifiedPaths);
return callback(err);
}

Expand Down
3 changes: 2 additions & 1 deletion test/versioning.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const mongoose = start.mongoose;
const Schema = mongoose.Schema;
const VersionError = mongoose.Error.VersionError;

describe('versioning', function() {
describe.only('versioning', function() {
var db;
var Comments;
var BlogPost;
Expand Down Expand Up @@ -270,6 +270,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