Meteor-Community-Packages / meteor-collection2

A Meteor package that extends Mongo.Collection to provide support for specifying a schema and then validating against that schema when inserting and updating.
https://packosphere.com/aldeed/collection2
MIT License
1.02k stars 108 forks source link

Duplicate key errors not correctly reporting (no Meteor.Error) #410

Open antislash21 opened 4 years ago

antislash21 commented 4 years ago

I currently use unique index on a field in an application

Groups.schema = new SimpleSchema(
  {
    name: {
      type: String,
      index: true,
      unique: true,
      min: 1,
    },
   ...
}

When inserting or updating in the table with an already existing name, I don't get a [40X] error with details.

On the server, i get the following kind of error while updating

I20200120-16:37:55.094(1)? Exception while invoking method 'groups.updateGroup' MongoError: E11000 duplicate key error collection: meteor.groups index: c2_name dup key: { : "Dev" }
I20200120-16:37:55.152(1)?     at Function.create (/home/bruno/.meteor/packages/npm-mongo/.3.3.0.1c4v1js.kcpb++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb-core/lib/error.js:43:12)
...

and while inserting

I20200120-16:37:55.155(1)? Exception while invoking method 'groups.createGroup' BulkWriteError: E11000 duplicate key error collection: meteor.groups index: c2_name dup key: { : "Tous" }
I20200120-16:37:55.156(1)?     at OrderedBulkOperation.handleWriteError (/home/bruno/.meteor/packages/npm-mongo/.3.3.0.1c4v1js.kcpb++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb/lib/bulk/common.js:1112:11)
...

On client side this results in a 500 internal error with no details

I see in collection2 code that these kind of errors should result in Meteor.Error with error 409, this doesn't seem to be the case.

I upgraded to these versions before reporting this

meteor version 1.9.0 simpl-schema 1.5.6 aldeed:collection2 3.0.6 aldeed:schema-index 3.0.0

Thanks for your work and time spent here

coagmano commented 4 years ago

I think server side errors are 500s by default to limit information exposure, but you can overwrite this behaviour in SimpleSchema by using the transform hook to allow it:

SimpleSchema.defineValidationErrorTransform(error => {
    const ddpError = new Meteor.Error(error.message);
    ddpError.error = 'validation-error';
    ddpError.details = error.details;
    return ddpError;
});

I'm not 100% sure on this though, but this snippet is what we use so 🤷‍♂

antislash21 commented 4 years ago

@coagmano : I already tried to include this on server startup without success. It seems the Error is not processed by SimpleSchema. As you can see in my example, my method ends up with an exception coming directly from npm-mongo library.

If this can help, the application code is here: https://gitlab.mim-libre.fr/alphabet/laboite

schema is defined in app/imports/api/groups/groups.js the methods called are createGroup and updateGroup from app/imports/api/groups/methods.js there are unit tests in app/imports/api/groups/server/groups.tests.js (you can search for E11000 duplicate key).

cfesas01 commented 2 years ago

can confirm the same issue as this.