Skip to content

Commit 8a161df

Browse files
committed
fix
1 parent 3fb8f77 commit 8a161df

File tree

2 files changed

+12
-27
lines changed

2 files changed

+12
-27
lines changed

spec/schemas.spec.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3842,6 +3842,8 @@ describe('schemas', () => {
38423842
});
38433843

38443844
it_id('cbd5d897-b938-43a4-8f5a-5d02dd2be9be')(it_exclude_dbs(['postgres']))('cannot update to duplicate value on unique index', done => {
3845+
const logger = require('../lib/logger').default;
3846+
const loggerErrorSpy = spyOn(logger, 'error').and.callThrough();
38453847
const index = {
38463848
code: 1,
38473849
};
@@ -3868,7 +3870,12 @@ describe('schemas', () => {
38683870
.then(done.fail)
38693871
.catch(error => {
38703872
expect(error.code).toEqual(Parse.Error.DUPLICATE_VALUE);
3871-
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')
3873+
// Client should only see generic message (no schema info exposed)
3874+
expect(error.message).toEqual('A duplicate value for a field with unique values was provided');
3875+
// Server logs should contain full MongoDB error message with detailed information
3876+
expect(loggerErrorSpy).toHaveBeenCalledWith('Duplicate key error:', jasmine.stringContaining('E11000 duplicate key error'));
3877+
expect(loggerErrorSpy).toHaveBeenCalledWith('Duplicate key error:', jasmine.stringContaining('test_UniqueIndexClass'));
3878+
expect(loggerErrorSpy).toHaveBeenCalledWith('Duplicate key error:', jasmine.stringContaining('code_1'));
38723879
done();
38733880
});
38743881
});

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

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

2828
const MongoSchemaCollectionName = '_SCHEMA';
2929

30-
31-
// If we get a unique index error from mongo, try to parse it. If successful, return it.
32-
const mongoUniqueIndexErrorFormatter = (message) => {
33-
/**
34-
* Sample error message that we are getting from mongo
35-
* 'Plan executor error during findAndModify :: caused by :: E11000 duplicate key error collection: parseServerMongoAdapterTestDatabase.test_UniqueIndexClass index: code_1 dup key: { code: 2 }'
36-
*/
37-
const regex = /collection:\s*([\w-]+)\.([\w-]+)\s+index:\s*([\w-]+)/;
38-
39-
const match = message.match(regex);
40-
41-
if (match) {
42-
const dbName = match[1];
43-
const collectionName = match[2];
44-
const indexName = match[3];
45-
// Adding extra starting space to make it more readable.
46-
return ` Duplicate index: ${indexName} on collection ${collectionName} in db ${dbName}`;
47-
}
48-
49-
// Return nothing
50-
return '';
51-
}
52-
5330
const storageAdapterAllCollections = mongoAdapter => {
5431
return mongoAdapter
5532
.connect()
@@ -542,10 +519,10 @@ export class MongoStorageAdapter implements StorageAdapter {
542519
.then(() => ({ ops: [mongoObject] }))
543520
.catch(error => {
544521
if (error.code === 11000) {
545-
// Duplicate value
522+
logger.error('Duplicate key error:', error.message);
546523
const err = new Parse.Error(
547524
Parse.Error.DUPLICATE_VALUE,
548-
`A duplicate value for a field with unique values was provided.${mongoUniqueIndexErrorFormatter(error.message)}`
525+
'A duplicate value for a field with unique values was provided'
549526
);
550527
err.underlyingError = error;
551528
if (error.message) {
@@ -628,9 +605,10 @@ export class MongoStorageAdapter implements StorageAdapter {
628605
.then(result => mongoObjectToParseObject(className, result, schema))
629606
.catch(error => {
630607
if (error.code === 11000) {
608+
logger.error('Duplicate key error:', error.message);
631609
throw new Parse.Error(
632610
Parse.Error.DUPLICATE_VALUE,
633-
`A duplicate value for a field with unique values was provided.${mongoUniqueIndexErrorFormatter(error.message)}`
611+
'A duplicate value for a field with unique values was provided'
634612
);
635613
}
636614
throw error;

0 commit comments

Comments
 (0)