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

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

Open
antislash21 opened this issue Jan 20, 2020 · 3 comments
Open

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

antislash21 opened this issue Jan 20, 2020 · 3 comments

Comments

@antislash21
Copy link

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
Copy link
Member

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
Copy link
Author

@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
Copy link

can confirm the same issue as this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

4 participants