Skip to content

Commit 0e0dba0

Browse files
authored
Merge pull request #6852 from hellodigit/4.x-version-number-errors
feat(error): add version number to VersionError (4.x)
2 parents 471cadc + ce7fb11 commit 0e0dba0

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

lib/error/version.js

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

16-
function VersionError(doc) {
16+
function VersionError(doc, currentVersion) {
1717
MongooseError.call(this, 'No matching document found for id "' + doc._id +
18-
'"');
18+
'" version ' + currentVersion);
1919
this.name = 'VersionError';
20+
this.version = currentVersion;
2021
}
2122

2223
/*!

lib/model.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,17 @@ Model.prototype.$__save = function(options, callback) {
258258
var doIncrement = VERSION_INC === (VERSION_INC & _this.$__.version);
259259
_this.$__.version = undefined;
260260

261+
var key = _this.schema.options.versionKey;
262+
var version = _this.getValue(key) || 0;
263+
261264
if (numAffected <= 0) {
262265
// the update failed. pass an error back
263-
var err = new VersionError(_this);
266+
var err = new VersionError(_this, version);
264267
return callback(err);
265268
}
266269

267270
// increment version if was successful
268271
if (doIncrement) {
269-
var key = _this.schema.options.versionKey;
270-
var version = _this.getValue(key) | 0;
271272
_this.setValue(key, version + 1);
272273
}
273274
}

test/versioning.test.js

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

0 commit comments

Comments
 (0)