Skip to content

Commit

Permalink
ADD helpfull error message for #5046
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Oct 5, 2023
1 parent ae84f68 commit 45285ec
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/plugins/dev-mode/error-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export const ERROR_MESSAGES = {
DOC21: 'PrimaryKey must be equal to PrimaryKey.trim(). It cannot start or end with a whitespace',
DOC22: 'PrimaryKey must not contain a linebreak',
DOC23: 'PrimaryKey must not contain a double-quote ["]',
DOC24: 'Given document data could not be structured cloned. This happens if you pass non-plain-json data into it, like a Date() or a Function. ' +
'In vue.js this happens if you use ref() on the document data which transforms it into a Proxy object.',

// data-migrator.js
DM1: 'migrate() Migration has already run',
Expand Down
14 changes: 14 additions & 0 deletions src/rx-storage-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,20 @@ export function getWrappedStorageInstance<
data as any
);


/**
* Ensure it can be structured cloned
*/
try {
structuredClone(writeRow);
} catch (err) {
throw newRxError('DOC24', {
collection: storageInstance.collectionName,
document: writeRow.document
});
}


/**
* Ensure that the new revision is higher
* then the previous one
Expand Down
24 changes: 24 additions & 0 deletions test/unit/rx-collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,30 @@ describe('rx-collection.test.ts', () => {
].map(id => ensurePrimaryKeyInsertThrows(id)));
c.database.destroy();
});
/**
* @link https://github.com/pubkey/rxdb/issues/5046
*/
it('should throw a helpful error on non-plain-json data', async () => {
const c = await humansCollection.create(1);

// inserts
const doc = schemaObjects.human();
(doc as any).lastName = () => { };
await assertThrows(
() => c.insert(doc),
'RxError',
'DOC24'
);

// updates
const doc2 = await c.findOne().exec(true);
await assertThrows(
() => doc2.patch({ lastName: (() => { }) as any }),
'RxError',
'DOC24'
);
c.database.destroy();
});
});
});
config.parallel('.bulkInsert()', () => {
Expand Down

0 comments on commit 45285ec

Please sign in to comment.