Skip to content

Commit 7ba0068

Browse files
pagliasfreewil
authored andcommitted
feat(error): add modified paths to VersionError
backport #6464 to 4.x
1 parent 631f476 commit 7ba0068

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

lib/error/version.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ var MongooseError = require('./');
1313
* @api private
1414
*/
1515

16-
function VersionError(doc, currentVersion) {
16+
function VersionError(doc, currentVersion, modifiedPaths) {
17+
var modifiedPathsStr = modifiedPaths.join(', ');
1718
MongooseError.call(this, 'No matching document found for id "' + doc._id +
18-
'" version ' + currentVersion);
19+
'" version ' + currentVersion + 'modifiedPaths "' + modifiedPathsStr + '"');
1920
this.name = 'VersionError';
2021
this.version = currentVersion;
22+
this.modifiedPaths = modifiedPaths;
2123
}
2224

2325
/*!

lib/model.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ Model.prototype.$__save = function(options, callback) {
229229
});
230230
}
231231

232+
// store the modified paths before the document is reset
233+
var modifiedPaths = _this.modifiedPaths();
234+
232235
_this.$__reset();
233236

234237
var numAffected = 0;
@@ -263,7 +266,7 @@ Model.prototype.$__save = function(options, callback) {
263266

264267
if (numAffected <= 0) {
265268
// the update failed. pass an error back
266-
var err = new VersionError(_this, version);
269+
var err = new VersionError(_this, version, modifiedPaths);
267270
return callback(err);
268271
}
269272

test/versioning.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ describe('versioning', function() {
266266
assert.ok(/No matching document/.test(err), err);
267267
assert.equal(a._doc.__v, 5);
268268
assert.equal(err.version, b._doc.__v - 1);
269+
assert.deepEqual(err.modifiedPaths, ['numbers', 'numbers.2']);
269270
a.set('arr.0.0', 'updated');
270271
var d = a.$__delta();
271272
assert.equal(a._doc.__v, d[0].__v, 'version should be added to where clause');

0 commit comments

Comments
 (0)