fix(core): Support WiredTiger engine errmsg format in MongoDB 3.2

The new WiredTiger engine is introduced in MongoDB 3.2.
It changes the output errmsg format for violation of unique index.
This commit adds support for the new format.

Fixes #1245
This commit is contained in:
Qiyu Li
2016-03-03 11:21:36 -05:00
parent 049fde9451
commit 6265aaa7da
2 changed files with 13 additions and 9 deletions

View File

@@ -7,7 +7,17 @@ var getUniqueErrorMessage = function (err) {
var output;
try {
var fieldName = err.errmsg.substring(err.errmsg.lastIndexOf('.$') + 2, err.errmsg.lastIndexOf('_1'));
var begin = 0;
if (err.errmsg.lastIndexOf('.$') !== -1) {
// support mongodb <= 3.0 (default: MMapv1 engine)
// "errmsg" : "E11000 duplicate key error index: mean-dev.users.$email_1 dup key: { : \"test@user.com\" }"
begin = err.errmsg.lastIndexOf('.$') + 2;
} else {
// support mongodb >= 3.2 (default: WiredTiger engine)
// "errmsg" : "E11000 duplicate key error collection: mean-dev.users index: email_1 dup key: { : \"test@user.com\" }"
begin = err.errmsg.lastIndexOf('index: ') + 7;
}
var fieldName = err.errmsg.substring(begin, err.errmsg.lastIndexOf('_1'));
output = fieldName.charAt(0).toUpperCase() + fieldName.slice(1) + ' already exists';
} catch (ex) {

View File

@@ -742,10 +742,7 @@ describe('User CRUD tests', function () {
}
// Call the assertion callback
// MongoDB changed document validation output in version 3.2:
// >= 3.2 11000 duplicate key error collection: mean-test.users index: username already exists
// < 3.2 Username already exists
userInfoRes.body.message.toLowerCase().should.containEql('username already exists');
userInfoRes.body.message.should.equal('Username already exists');
return done();
});
@@ -797,10 +794,7 @@ describe('User CRUD tests', function () {
}
// Call the assertion callback
// MongoDB changed document validation output in version 3.2:
// >= 3.2 11000 duplicate key error collection: mean-test.users index: email already exists
// < 3.2 Email already exists
userInfoRes.body.message.toLowerCase().should.containEql('email already exists');
userInfoRes.body.message.should.equal('Email already exists');
return done();
});