diff --git a/lib/error-handler.js b/lib/error-handler.js index a93ba276..df8a6b2e 100644 --- a/lib/error-handler.js +++ b/lib/error-handler.js @@ -5,7 +5,7 @@ module.exports = function errorHandler (error) { // NOTE (EK): Error parsing as discussed in this github thread // https://github.com/Automattic/mongoose/issues/2129 const match1 = error.message.match(/_?([a-zA-Z]*)_?\d?\s*dup key/i); - const match2 = error.message.match(/\s*dup key:\s*\{\s*:\s*"?(\S+)"?\s*\}/i); + const match2 = error.message.match(/\s*dup key:\s*\{\s*:\s*"?(.*?)"?\s*\}/i); const key = match1 ? match1[1] : 'path'; let value = match2 ? match2[1] : 'value'; diff --git a/test/error-handler.test.js b/test/error-handler.test.js index e3adc647..7a273b86 100644 --- a/test/error-handler.test.js +++ b/test/error-handler.test.js @@ -125,7 +125,17 @@ describe('Feathers Mongoose Error Handler', () => { }).catch(done); }); - it('has the correct error message', done => { + it('has the correct error message #1', done => { + let e = Error('E11000 duplicate key error collection: db.users index: name_1 dup key: { : "Kate" }'); + e.name = 'MongoError'; + e.code = 11000; + errorHandler(e).catch(error => { + expect(error.message).to.equal(`name: Kate already exists.`); + done(); + }).catch(done); + }); + + it('has the correct error message #2', done => { let e = Error("E11000 duplicate key error index: myDb.myCollection.$id dup key: { : ObjectId('57226808ec55240c00000272') }"); e.name = 'MongoError'; e.code = 11000; @@ -135,7 +145,7 @@ describe('Feathers Mongoose Error Handler', () => { }).catch(done); }); - it('has the correct errors object', done => { + it('has the correct errors object #1', done => { let e = Error('E11000 duplicate key error index: test.collection.$a.b_1 dup key: { : null }'); e.name = 'MongoError'; e.code = 11000; @@ -144,5 +154,15 @@ describe('Feathers Mongoose Error Handler', () => { done(); }).catch(done); }); + + it('has the correct errors object #2', done => { + let e = Error('E11000 duplicate key error collection: db.users index: name_1 dup key: { : "Kate" }'); + e.name = 'MongoError'; + e.code = 11000; + errorHandler(e).catch(error => { + expect(error.errors).to.deep.equal({ name: 'Kate' }); + done(); + }).catch(done); + }); }); });