title: Catching a duplicate error from mongoose unique index and sending a res 409
body: <p>I have a mongoose schema where I define an email to be unique. When I insert a duplicate email I get a Mongoose error, which is what I expect, but I want to catch it and send a res.status(409) when that happens, and I can't do it.</p>
  
  <p>I've tried to use the <strong>mongoose-beautiful-unique-validation</strong> library to change the error I receive and catch it but I'm still lost.</p>
  
  <p>My schema: </p>
  
  <pre><code>'use strict';
  const beautifyUnique = require('mongoose-beautiful-unique-validation');
  
  const mongoose = require('mongoose');
  
  // create the schema
  
  const { Schema } = mongoose;
  
  const accountSchema = new Schema({
      uuid: {
          type: String,
          unique: true,
        },
        email: {
          type: String,
          unique: 'This email is already taken ({VALUE})',
        },
      password: String,
      createdAt: Date, 
      verification: {
        verificationCode: String,
        verifiedAt: Date,
      },
    });
  
  accountSchema.plugin(beautifyUnique);
  const Account = mongoose.model('Account', accountSchema);
  
  module.exports = Account;
  </code></pre>
  
  <p>The error I'm trying to catch:</p>
  
  <pre><code>
  { ValidationError: Validation failed
      at ValidationError.inspect (/home/danielromero/Desktop/foodtrack git clone/foodtrack-demo/backend_foodtrack/node_modules/mongoose/lib/error/validation.js:59:24)
      at formatValue (internal/util/inspect.js:453:31)
      at inspect (internal/util/inspect.js:193:10)
      at Object.formatWithOptions (util.js:165:18)
      at Console.(anonymous function) (console.js:188:15)
      at Console.log (console.js:199:31)
      at insertAccountIntoDatabase (/home/danielromero/Desktop/foodtrack git clone/foodtrack-demo/backend_foodtrack/webserver/controllers/account/create-account.js:36:13)
      at process._tickCallback (internal/process/next_tick.js:68:7)
    errors:
     { email:
        { ValidatorError: This email is already taken (sapo@yopmail.com)
            at new ValidatorError (/home/danielromero/Desktop/foodtrack git clone/foodtrack-demo/backend_foodtrack/node_modules/mongoose/lib/error/validator.js:29:11)
            at /home/danielromero/Desktop/foodtrack git clone/foodtrack-demo/backend_foodtrack/node_modules/mongoose-beautiful-unique-validation/index.js:150:39
            at Array.forEach (&lt;anonymous&gt;)
            at /home/danielromero/Desktop/foodtrack git clone/foodtrack-demo/backend_foodtrack/node_modules/mongoose-beautiful-unique-validation/index.js:140:36
            at process._tickCallback (internal/process/next_tick.js:68:7)
          message: 'This email is already taken (sapo@yopmail.com)',
          name: 'ValidatorError',
          properties: [Object],
          kind: 'unique',
          path: 'email',
          value: 'sapo@yopmail.com',
          reason: undefined } },
    _message: 'Validation failed',
    name: 'ValidationError' }
  </code></pre>
  
  <p>I just want to know how can I reference the ValidationError message, and tell my code in a middleware that when that happens it has to send a 409 response.</p>