Skip to content

Commit

Permalink
Issue meteor#13121 updated the error handling within the Meteor metho…
Browse files Browse the repository at this point in the history
…d for inserting data into a MongoDB collection
  • Loading branch information
Sageyuva committed May 4, 2024
1 parent 02aa69c commit 65daf95
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/allow-deny/allow-deny.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,19 @@ CollectionPrototype._defineMutationMethods = function(options) {
}
} catch (e) {
if (
e.name === 'MongoError' ||
e.name === 'MongoError' && e.code === 11000 ||

This comment has been minimized.

Copy link
@tanase-sebastian

tanase-sebastian May 6, 2024

I'm not sure this test will actually pass. The name is MongoServerError so the first part of the test will still be false. I was thinking of a "simpler" fix which is just replacing MongoError with MongoServerError, without touching the if block underneath.

// for old versions of MongoDB (probably not necessary but it's here just in case)
e.name === 'BulkWriteError' ||
// for newer versions of MongoDB (https://docs.mongodb.com/drivers/node/current/whats-new/#bulkwriteerror---mongobulkwriteerror)
e.name === 'MongoBulkWriteError' ||
e.name === 'MinimongoError'
) {
throw new Meteor.Error(409, e.toString());
// Extract the index name from the error message
const match = e.errmsg.match(/index: (.+?) dup key/);
const indexName = match ? match[1] : 'unknown';

// Throw a specific Meteor.Error with the index name
throw new Meteor.Error(409, `Duplicate key error on index: ${indexName}`);
} else {
throw e;
}
Expand Down

0 comments on commit 65daf95

Please sign in to comment.