Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Commit

Permalink
fix(core): Support WiredTiger engine errmsg format in MongoDB 3.2
Browse files Browse the repository at this point in the history
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
  • Loading branch information
QiyuLi committed Mar 4, 2016
1 parent 049fde9 commit 6265aaa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
12 changes: 11 additions & 1 deletion modules/core/server/controllers/errors.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 2 additions & 8 deletions modules/users/tests/server/user.server.routes.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
});
Expand Down

0 comments on commit 6265aaa

Please sign in to comment.