title: Better ways to handle MongoDB exceptions
body: <p>I am working on a MongoDB-backed authentication module for my asp.net application. The MongoDB database has a collections of users where I store login information - username, email and password. Both username and email are set to be unique: </p>
  
  <pre><code>users.ensureIndex({email:1}, {unique:1})
  users.ensureIndex({uname:1}, {unique:1})
  </code></pre>
  
  <p>Now, if I try to insert a record with existing uname or email, i get a MongoDB exception:</p>
  
  <pre><code>Safemode detected an error: E11000 duplicate key error index: 
  authdb.users.$email_1  dup key: { : "test@test.com" } 
  (response: { "err" : "E11000 duplicate key error index: authdb.users.$email_1  
  dup key: { : \"test@test.com\" }", "code" : 11000, "n" : 0, "connectionId" : 9, 
  "ok" : 1.0 })
  </code></pre>
  
  <p>I need to tell the user that the username or email they entered already exists, but the whole exception is just a blob of text, and to see what's going on I have to guess by looking whether "$email" or "$uname" are in the error message text. Is there some sort of parser for MongoDB exceptions which would help me detect what the exception is for?</p>