feat: Log more debug info when failing to set duplicate value for field with unique values (#9919)

This commit is contained in:
Rahul Lanjewar
2025-12-14 21:09:17 +05:30
committed by GitHub
parent 98a42e5277
commit a23b192466
2 changed files with 9 additions and 1 deletions

View File

@@ -3842,6 +3842,7 @@ describe('schemas', () => {
}); });
it_id('cbd5d897-b938-43a4-8f5a-5d02dd2be9be')(it_exclude_dbs(['postgres']))('cannot update to duplicate value on unique index', done => { it_id('cbd5d897-b938-43a4-8f5a-5d02dd2be9be')(it_exclude_dbs(['postgres']))('cannot update to duplicate value on unique index', done => {
loggerErrorSpy.calls.reset();
const index = { const index = {
code: 1, code: 1,
}; };
@@ -3868,6 +3869,12 @@ describe('schemas', () => {
.then(done.fail) .then(done.fail)
.catch(error => { .catch(error => {
expect(error.code).toEqual(Parse.Error.DUPLICATE_VALUE); expect(error.code).toEqual(Parse.Error.DUPLICATE_VALUE);
// Client should only see generic message (no schema info exposed)
expect(error.message).toEqual('A duplicate value for a field with unique values was provided');
// Server logs should contain full MongoDB error message with detailed information
expect(loggerErrorSpy).toHaveBeenCalledWith('Duplicate key error:', jasmine.stringContaining('E11000 duplicate key error'));
expect(loggerErrorSpy).toHaveBeenCalledWith('Duplicate key error:', jasmine.stringContaining('test_UniqueIndexClass'));
expect(loggerErrorSpy).toHaveBeenCalledWith('Duplicate key error:', jasmine.stringContaining('code_1'));
done(); done();
}); });
}); });

View File

@@ -519,7 +519,7 @@ export class MongoStorageAdapter implements StorageAdapter {
.then(() => ({ ops: [mongoObject] })) .then(() => ({ ops: [mongoObject] }))
.catch(error => { .catch(error => {
if (error.code === 11000) { if (error.code === 11000) {
// Duplicate value logger.error('Duplicate key error:', error.message);
const err = new Parse.Error( const err = new Parse.Error(
Parse.Error.DUPLICATE_VALUE, Parse.Error.DUPLICATE_VALUE,
'A duplicate value for a field with unique values was provided' 'A duplicate value for a field with unique values was provided'
@@ -605,6 +605,7 @@ export class MongoStorageAdapter implements StorageAdapter {
.then(result => mongoObjectToParseObject(className, result, schema)) .then(result => mongoObjectToParseObject(className, result, schema))
.catch(error => { .catch(error => {
if (error.code === 11000) { if (error.code === 11000) {
logger.error('Duplicate key error:', error.message);
throw new Parse.Error( throw new Parse.Error(
Parse.Error.DUPLICATE_VALUE, Parse.Error.DUPLICATE_VALUE,
'A duplicate value for a field with unique values was provided' 'A duplicate value for a field with unique values was provided'