From 45285ec5f981975181fa40b84c6eb64e74b1499d Mon Sep 17 00:00:00 2001 From: pubkey <8926560+pubkey@users.noreply.github.com> Date: Thu, 5 Oct 2023 13:05:39 +0200 Subject: [PATCH] ADD helpfull error message for #5046 --- src/plugins/dev-mode/error-messages.ts | 2 ++ src/rx-storage-helper.ts | 14 ++++++++++++++ test/unit/rx-collection.test.ts | 24 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/plugins/dev-mode/error-messages.ts b/src/plugins/dev-mode/error-messages.ts index 1ba14b0bcf9..d3ad5dc9399 100644 --- a/src/plugins/dev-mode/error-messages.ts +++ b/src/plugins/dev-mode/error-messages.ts @@ -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', diff --git a/src/rx-storage-helper.ts b/src/rx-storage-helper.ts index 827c20bf9ff..6111c27d5e1 100644 --- a/src/rx-storage-helper.ts +++ b/src/rx-storage-helper.ts @@ -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 diff --git a/test/unit/rx-collection.test.ts b/test/unit/rx-collection.test.ts index 5dd19c64f5f..290aa09fc79 100644 --- a/test/unit/rx-collection.test.ts +++ b/test/unit/rx-collection.test.ts @@ -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()', () => {