Make error message unique

Summary: We're currently getting errors for every duplicate key and can't easily unify them, so we're adding the additional information to a warning instead.

Reviewed By: mweststrate

Differential Revision: D30337821

fbshipit-source-id: db9dc44d7d3424de169bed9b4447b482e411eb19
This commit is contained in:
Pascal Hartig
2021-08-16 05:31:37 -07:00
committed by Facebook GitHub Bot
parent e880059167
commit 797007f367
4 changed files with 9 additions and 3 deletions

View File

@@ -212,7 +212,13 @@ export class DataSource<T> {
if (this.keyAttribute) {
const key = this.getKey(value);
if (this._recordsById.has(key)) {
throw new Error(`Duplicate key: '${key}'`);
const existingValue = this._recordsById.get(key);
console.warn(
`Tried to append value with duplicate key: ${key} (key attribute is ${this.keyAttribute}). Old/new values:`,
existingValue,
value,
);
throw new Error(`Duplicate key`);
}
this._recordsById.set(key, value);
this.storeIndexOfKey(key, this._records.length);

View File

@@ -116,7 +116,7 @@ test('throws on invalid keys', () => {
}).toThrow(`Invalid key value: ''`);
expect(() => {
ds.append({id: 'cookie', title: 'test'});
}).toThrow(`Duplicate key: 'cookie'`);
}).toThrow(`Duplicate key`);
});
test('throws on update causing duplicate key', () => {