@@ -26,6 +26,29 @@ const ReadPreference = mongodb.ReadPreference;
2626
2727const MongoSchemaCollectionName = '_SCHEMA' ;
2828
29+
30+ // If we get a unique index error from mongo, try to parse it. If successful, return it.
31+ const mongoUniqueIndexErrorFormatter = ( message ) => {
32+ /**
33+ * Sample error message that we are getting from mongo
34+ * 'Plan executor error during findAndModify :: caused by :: E11000 duplicate key error collection: parseServerMongoAdapterTestDatabase.test_UniqueIndexClass index: code_1 dup key: { code: 2 }'
35+ */
36+ const regex = / c o l l e c t i o n : \s * ( [ \w ] + ) \. ( [ \w ] + ) \s + i n d e x : \s * ( [ \w _ ] + ) / ;
37+
38+ const match = message . match ( regex ) ;
39+
40+ if ( match ) {
41+ const dbName = match [ 1 ] ;
42+ const collectionName = match [ 2 ] ;
43+ const indexName = match [ 3 ] ;
44+ // Adding extra starting space to make it more readable.
45+ return ` Duplicate index: ${ indexName } on collection ${ collectionName } in db ${ dbName } ` ;
46+ }
47+
48+ // Return nothing
49+ return '' ;
50+ }
51+
2952const storageAdapterAllCollections = mongoAdapter => {
3053 return mongoAdapter
3154 . connect ( )
@@ -490,7 +513,7 @@ export class MongoStorageAdapter implements StorageAdapter {
490513 // Duplicate value
491514 const err = new Parse . Error (
492515 Parse . Error . DUPLICATE_VALUE ,
493- ' A duplicate value for a field with unique values was provided'
516+ ` A duplicate value for a field with unique values was provided. ${ mongoUniqueIndexErrorFormatter ( error . message ) } `
494517 ) ;
495518 err . underlyingError = error ;
496519 if ( error . message ) {
@@ -575,7 +598,7 @@ export class MongoStorageAdapter implements StorageAdapter {
575598 if ( error . code === 11000 ) {
576599 throw new Parse . Error (
577600 Parse . Error . DUPLICATE_VALUE ,
578- ' A duplicate value for a field with unique values was provided'
601+ ` A duplicate value for a field with unique values was provided. ${ mongoUniqueIndexErrorFormatter ( error . message ) } `
579602 ) ;
580603 }
581604 throw error ;
0 commit comments