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

feat(error): add version number to VersionError (4.x) #6852

Merged
merged 1 commit into from
Aug 14, 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
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