Skip to content

Commit

Permalink
handle proxy object to safely stringify (#3213)
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityvi authored Jun 7, 2022
1 parent c259a57 commit 86cc5d6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/utils/safeStringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
21 changes: 21 additions & 0 deletions test/src/utils/testUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]"');
});

});

0 comments on commit 86cc5d6

Please sign in to comment.