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

adding better mongoose conflict error handling #168

Merged
merged 1 commit into from
Feb 27, 2017
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
18 changes: 17 additions & 1 deletion src/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,23 @@ export default function errorHandler (error) {
case 'DivergentArrayError':
return Promise.reject(new errors.GeneralError(error));
case 'MongoError':
if (error.code === 11000) {
if (error.code === 11000 || error.code === 11001) {
// NOTE (EK): Error parsing as discussed in this github thread
// https://github.com/Automattic/mongoose/issues/2129
const key = error.message.match(/_?([a-zA-Z]*)_?\d?\s*dup key/i)[1];
let value = error.message.match(/\s*dup key:\s*\{\s*:\s*"?([a-zA-Z0-9'().]+)"?\s*\}/i)[1];

if (value === 'null') {
value = null;
} else if (value === 'undefined') {
value = undefined;
}

error.message = `${key} '${value}' already exists.`;
error.errors = {
[key]: value
};

return Promise.reject(new errors.Conflict(error));
}

Expand Down
38 changes: 30 additions & 8 deletions test/error-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,35 @@ describe('Feathers Mongoose Error Handler', () => {
}).catch(done);
});

it('wraps a DuplicateKey error as a Conflict', done => {
let e = Error('Mock Duplicate Key Error');
e.name = 'MongoError';
e.code = 11000;
errorHandler(e).catch(error => {
expect(error).to.be.an.instanceof(errors.Conflict);
done();
}).catch(done);
describe('DuplicateKey error', () => {
it('gets wrapped as a Conflict error', 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).to.be.an.instanceof(errors.Conflict);
done();
}).catch(done);
});

it('has the correct error message', done => {
let e = Error("E11000 duplicate key error index: myDb.myCollection.$id dup key: { : ObjectId('57226808ec55240c00000272') }");
e.name = 'MongoError';
e.code = 11000;
errorHandler(e).catch(error => {
expect(error.message).to.equal(`id 'ObjectId('57226808ec55240c00000272')' already exists.`);
done();
}).catch(done);
});

it('has the correct errors object', done => {
let e = Error('E11000 duplicate key error index: test.collection.$a.b_1 dup key: { : null }');
e.name = 'MongoError';
e.code = 11000;
errorHandler(e).catch(error => {
expect(error.errors).to.deep.equal({ b: null });
done();
}).catch(done);
});
});
});