Skip to content

Commit 426608e

Browse files
committed
fix: wrapError to cache objects to deal with circular nesting
1 parent c97f284 commit 426608e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

packages/tools/wrapError.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export function debugObject (head) { return debugObjectNest(head); }
212212
export function debugObjectHard (head) { return debugObjectNest(head, 1, true); }
213213
export function debugObjectType (head) { return debugObjectNest(head, false, false); }
214214

215-
export function debugObjectNest (head, nest = 1, alwaysStringify = false) {
215+
export function debugObjectNest (head, nest = 1, alwaysStringify = false, cache_: ?Object) {
216216
try {
217217
if (head === null) return "<null>";
218218
if (head === undefined) return "<undefined>";
@@ -243,17 +243,23 @@ export function debugObjectNest (head, nest = 1, alwaysStringify = false) {
243243
return `<${(head.constructor && head.constructor.name) || "no-constructor"} keys.length=${
244244
Object.keys(head).length}>`;
245245
}
246-
return ((Array.isArray(head)
247-
&& `[${head.map(entry => debugObjectNest(entry, nest, alwaysStringify)).join(", ")}]`)
248-
|| (isIterable(head) && debugObjectNest(head.toJS(), nest, alwaysStringify))
249-
|| (head[Symbol.iterator] && debugObjectNest([...head], nest, alwaysStringify))
246+
const cache = cache_ || new Map();
247+
const circularIndex = cache.get(head);
248+
if (circularIndex) return `<Circular #${circularIndex}: ${debugObjectNest(head, 0)}>`;
249+
cache.set(head, (cache.objectIndex = (cache.objectIndex || 0) + 1));
250+
return ((Array.isArray(head) && `[${
251+
head.map(entry => debugObjectNest(entry, nest, alwaysStringify, cache)).join(", ")}]`)
252+
|| (isIterable(head) && debugObjectNest(head.toJS(), nest, alwaysStringify, cache))
253+
|| (head[Symbol.iterator] && debugObjectNest([...head], nest, alwaysStringify, cache))
250254
|| `{ ${Object.keys(head)
251255
.map(key => `${isSymbol(key) ? key.toString() : key}: ${
252-
debugObjectNest(head[key], nest - 1, alwaysStringify)}`)
256+
debugObjectNest(head[key], nest - 1, alwaysStringify, cache)}`)
253257
.join(", ")
254258
} }`);
255259
} catch (error) {
256-
console.error("Suppressed an error in debugObjectNest:", error,
260+
console.error("Suppressed an error in debugObjectNest:",
261+
"\n\terror.message:", error.message,
262+
"\n\terror.stack:", error.stack,
257263
"\n\treturning head:", head);
258264
}
259265
return head;

0 commit comments

Comments
 (0)