From 86cc5d6c8d63d98bbf9110173773097ff14611e7 Mon Sep 17 00:00:00 2001 From: Ravi Sawlani Date: Tue, 7 Jun 2022 13:19:54 +0530 Subject: [PATCH] handle proxy object to safely stringify (#3213) --- lib/utils/safeStringify.js | 12 ++++++++---- test/src/utils/testUtils.js | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/utils/safeStringify.js b/lib/utils/safeStringify.js index d99fd43dee..7d19ff2e19 100644 --- a/lib/utils/safeStringify.js +++ b/lib/utils/safeStringify.js @@ -10,10 +10,14 @@ class SafeStringify { seen.push(obj); if (typeof obj.toJSON === 'function') { - const result = this.visit(obj.toJSON()); - seen.pop(); - - return result; + try { + const result = this.visit(obj.toJSON(), seen); + seen.pop(); + + return result; + } catch (err) { + return '[Error]'; + } } if (Array.isArray(obj)) { const result = obj.map(val => this.visit(val, seen)); diff --git a/test/src/utils/testUtils.js b/test/src/utils/testUtils.js index e0606e8ae9..dca44a51a9 100644 --- a/test/src/utils/testUtils.js +++ b/test/src/utils/testUtils.js @@ -158,4 +158,25 @@ describe('test Utils', function() { assert.strictEqual(Utils.SafeJSON.stringify(obj), '{"value":1,"cirRef":"[Circular]"}'); }); + it('SafeJSON.stringify for Proxy objects', function() { + + const target = { + value: 1 + }; + + const proxyObj = new Proxy(target, { + get(target, property) { + return function(...args) { + if (!target[property]){ + throw new Error('Unknown property'); + } + + return target[property]; + }; + } + }); + + assert.strictEqual(Utils.SafeJSON.stringify(proxyObj), '"[Error]"'); + }); + });