How to set a custom error message for a unique index failure

6,193 views
Skip to first unread message

Eric Sorenson

unread,
Nov 17, 2011, 1:12:41 PM11/17/11
to Mongoose Node.JS ORM
When failing on a unique index, Mongoose returns an error like this:

{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'E11000 duplicate key error index: dev.usermodels.$email_1
dup key: { : "jeffrey....@me.com" }',
name: 'MongoError',
err: 'E11000 duplicate key error index: dev.usermodels.$email_1 dup
key: { : "jeffrey....@me.com" }',
code: 11000,
n: 0,
connectionId: 39,
ok: 1 }

I'd like to customize this with something more user friendly, like
"Email address already taken." I see that I can customize validation
error messages, but is it possible to do this with a unique index as
well?

Aaron Heckmann

unread,
Nov 18, 2011, 7:27:22 AM11/18/11
to mongoo...@googlegroups.com
yes but not the same way. that error is returned from mongodb so the first chance you have to handle it is in your callback.

thing.save(function (err) {
  if (err) {
    if (11000 === err.code || 11001 === err.code) { // or whatever

You'll get 11001 when saving an existing document that now has a unique key violation.
11000 is for new objects.


--
http://mongoosejs.com
http://github.com/learnboost/mongoose
You received this message because you are subscribed to the Google
Groups "Mongoose Node.JS ORM" group.
To post to this group, send email to mongoo...@googlegroups.com
To unsubscribe from this group, send email to
mongoose-orm...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/mongoose-orm?hl=en



--
Aaron


Eric Sorenson

unread,
Nov 19, 2011, 12:57:03 PM11/19/11
to Mongoose Node.JS ORM
Thanks, that helps, though this error only bubbles up after all of the
Mongoose validators pass. This makes sense, but it would be nice to
see all of the errors at the same time.

Am I going to have to write my own validator to check to see if the
email already exists, essentially duplicating the functionality of the
unique index?

On Nov 18, 6:27 am, Aaron Heckmann <aaron.heckm...@gmail.com> wrote:
> yes but not the same way. that error is returned from mongodb so the first
> chance you have to handle it is in your callback.
>
> thing.save(function (err) {
>   if (err) {
>     if (11000 === err.code || 11001 === err.code) { // or whatever
>
> You'll get 11001 when saving an existing document that now has a unique key
> violation.
> 11000 is for new objects.
>
> On Thu, Nov 17, 2011 at 1:12 PM, Eric Sorenson <
>
>
>
>
>
>
>
>
>
> eric.soren...@dubharmonic.com> wrote:
> > When failing on a unique index, Mongoose returns an error like this:
>
> > { stack: [Getter/Setter],
> >  arguments: undefined,
> >  type: undefined,
> >  message: 'E11000 duplicate key error index: dev.usermodels.$email_1

> > dup key: { : "jeffrey.lebow...@me.com" }',


> >  name: 'MongoError',
> >  err: 'E11000 duplicate key error index: dev.usermodels.$email_1  dup

> > key: { : "jeffrey.lebow...@me.com" }',

Jon Keating

unread,
Nov 19, 2011, 4:11:37 PM11/19/11
to mongoo...@googlegroups.com
That is what I ended up doing:

User.path('email').validate(function(v, fn) {
  // Make sure the email address is not already registered
  var UserModel = mongoose.model('User');
  UserModel.find({'email': v.toLowerCase()}, function (err, emails) {
    fn(err || emails.length === 0);
  });
}, 'Email is already registered');

Eric Sorenson

unread,
Nov 21, 2011, 11:03:06 AM11/21/11
to Mongoose Node.JS ORM
Jon - this is exactly what I was referring to! Thanks for posting the
code.

Aaron Heckmann

unread,
Nov 21, 2011, 3:53:29 PM11/21/11
to mongoo...@googlegroups.com
FWIW In our app I've seen this approach fail once a week or so but relying on the mongodb unique index violation error has proven reliable. I do not know why the query approach would fail but it sometimes does for us.

--
Aaron


Dan MacTough

unread,
Nov 22, 2011, 12:09:09 AM11/22/11
to mongoo...@googlegroups.com
Race condition?

Aaron Heckmann

unread,
Nov 22, 2011, 7:07:00 AM11/22/11
to mongoo...@googlegroups.com
On Tue, Nov 22, 2011 at 12:09 AM, Dan MacTough <danma...@gmail.com> wrote:
Race condition?

Yes, but we've never been able to reproduce it. Production is awesome.

--
Aaron


Reply all
Reply to author
Forward
0 new messages