Skip to content

Commit

Permalink
Merge pull request #6852 from hellodigit/4.x-version-number-errors
Browse files Browse the repository at this point in the history
feat(error): add version number to VersionError (4.x)
  • Loading branch information
vkarpov15 committed Aug 14, 2018
2 parents 471cadc + ce7fb11 commit 0e0dba0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/error/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ var MongooseError = require('./');
* @api private
*/

function VersionError(doc) {
function VersionError(doc, currentVersion) {
MongooseError.call(this, 'No matching document found for id "' + doc._id +
'"');
'" version ' + currentVersion);
this.name = 'VersionError';
this.version = currentVersion;
}

/*!
Expand Down
7 changes: 4 additions & 3 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,17 @@ Model.prototype.$__save = function(options, callback) {
var doIncrement = VERSION_INC === (VERSION_INC & _this.$__.version);
_this.$__.version = undefined;

var key = _this.schema.options.versionKey;
var version = _this.getValue(key) || 0;

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

// increment version if was successful
if (doIncrement) {
var key = _this.schema.options.versionKey;
var version = _this.getValue(key) | 0;
_this.setValue(key, version + 1);
}
}
Expand Down
1 change: 1 addition & 0 deletions test/versioning.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ describe('versioning', function() {
function test4(err, a, b) {
assert.ok(/No matching document/.test(err), err);
assert.equal(a._doc.__v, 5);
assert.equal(err.version, b._doc.__v - 1);
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 0e0dba0

Please sign in to comment.