{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/2284","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/2284/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/2284/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/2284/events","html_url":"https://github.com/Automattic/mongoose/issues/2284","id":42149702,"number":2284,"title":"beautiful mongodb native errors (unique - code 11000)","user":{"login":"xpepermint","id":174279,"avatar_url":"https://avatars.githubusercontent.com/u/174279?v=3","gravatar_id":"","url":"https://api.github.com/users/xpepermint","html_url":"https://github.com/xpepermint","followers_url":"https://api.github.com/users/xpepermint/followers","following_url":"https://api.github.com/users/xpepermint/following{/other_user}","gists_url":"https://api.github.com/users/xpepermint/gists{/gist_id}","starred_url":"https://api.github.com/users/xpepermint/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xpepermint/subscriptions","organizations_url":"https://api.github.com/users/xpepermint/orgs","repos_url":"https://api.github.com/users/xpepermint/repos","events_url":"https://api.github.com/users/xpepermint/events{/privacy}","received_events_url":"https://api.github.com/users/xpepermint/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Automattic/mongoose/labels/plugin","name":"plugin","color":"5319e7"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":14,"created_at":"2014-09-07T19:22:28Z","updated_at":"2015-05-17T11:39:32Z","closed_at":null,"body":"I really miss a mechanism that would create validator-like `unique index` error message. Setting the index of an attribute to `unique: true` will tell mongodb to return ugly error message like this:\r\n\r\n```\r\n{ [MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb-api.users.$email_1  dup key: { : \"my@duplicate.com\" }]\r\n  name: 'MongoError',\r\n  code: 11000,\r\n  err: 'insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb-api.users.$email_1  dup key: { : \"my@duplicate.com\" }' }\r\n```\r\n\r\nThis message should be treated as validator error message thus we could easily display error messages on a REST service.\r\n\r\n```js\r\n// koa example\r\nindex: function*() {\r\n    try {\r\n      var email = new User(body);\r\n      this.body = yield email.save();\r\n    } catch(e) {\r\n      this.body = e; // nice json errors to display\r\n    }\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/102786618","html_url":"https://github.com/Automattic/mongoose/issues/2284#issuecomment-102786618","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/2284","id":102786618,"user":{"login":"iliakan","id":349336,"avatar_url":"https://avatars.githubusercontent.com/u/349336?v=3","gravatar_id":"","url":"https://api.github.com/users/iliakan","html_url":"https://github.com/iliakan","followers_url":"https://api.github.com/users/iliakan/followers","following_url":"https://api.github.com/users/iliakan/following{/other_user}","gists_url":"https://api.github.com/users/iliakan/gists{/gist_id}","starred_url":"https://api.github.com/users/iliakan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iliakan/subscriptions","organizations_url":"https://api.github.com/users/iliakan/orgs","repos_url":"https://api.github.com/users/iliakan/repos","events_url":"https://api.github.com/users/iliakan/events{/privacy}","received_events_url":"https://api.github.com/users/iliakan/received_events","type":"User","site_admin":false},"created_at":"2015-05-17T11:39:32Z","updated_at":"2015-05-17T11:39:32Z","body":"My code is a little bit more versatile, so I stick to it for now, but thanks anyway ;)"}}