From 6265aaa7daeabf911a72c434f5b68226b9a4bdeb Mon Sep 17 00:00:00 2001 From: Qiyu Li Date: Thu, 3 Mar 2016 11:21:36 -0500 Subject: [PATCH] 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 --- .../server/controllers/errors.server.controller.js | 12 +++++++++++- .../users/tests/server/user.server.routes.tests.js | 10 ++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/modules/core/server/controllers/errors.server.controller.js b/modules/core/server/controllers/errors.server.controller.js index f0b96bdfb7..003d4aab5b 100644 --- a/modules/core/server/controllers/errors.server.controller.js +++ b/modules/core/server/controllers/errors.server.controller.js @@ -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) { diff --git a/modules/users/tests/server/user.server.routes.tests.js b/modules/users/tests/server/user.server.routes.tests.js index abd9795dc2..5d57c65e3b 100644 --- a/modules/users/tests/server/user.server.routes.tests.js +++ b/modules/users/tests/server/user.server.routes.tests.js @@ -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(); });