Skip to content

Commit 8cfd8ff

Browse files
feat: add information about, db collection and index name
1 parent 0b606ae commit 8cfd8ff

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

spec/schemas.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3835,6 +3835,7 @@ describe('schemas', () => {
38353835
.then(done.fail)
38363836
.catch(error => {
38373837
expect(error.code).toEqual(Parse.Error.DUPLICATE_VALUE);
3838+
expect(error.message).toEqual('A duplicate value for a field with unique values was provided Duplicate index: code_1 on collection test_UniqueIndexClass in db parseServerMongoAdapterTestDatabase')
38383839
done();
38393840
});
38403841
});

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,29 @@ const ReadPreference = mongodb.ReadPreference;
2626

2727
const 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 = /collection:\s*([\w]+)\.([\w]+)\s+index:\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+
2952
const 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

Comments
 (0)