Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,18 @@ export interface LogEntry {
}

/** @internal */
function removeCircular(obj: any, refs: any[] = []): any {
function removeCircular(obj: any, refs: Set<any> = new Set()): any {
if (typeof obj !== "object" || !obj) {
return obj;
}
// If the object defines its own toJSON, prefer that.
if (obj.toJSON) {
if (obj.toJSON && typeof obj.toJSON === "function") {
return obj.toJSON();
}
if (refs.includes(obj)) {
if (refs.has(obj)) {
return "[Circular]";
} else {
refs.push(obj);
refs.add(obj);
}
let returnObj: any;
if (Array.isArray(obj)) {
Expand All @@ -72,13 +72,21 @@ function removeCircular(obj: any, refs: any[] = []): any {
returnObj = {};
}
for (const k in obj) {
if (refs.includes(obj[k])) {
returnObj[k] = "[Circular]";
if (obj.hasOwnProperty(k)) {
try {
if (refs.has(obj[k])) {
returnObj[k] = "[Circular]";
} else {
returnObj[k] = removeCircular(obj[k], refs);
}
} catch {
returnObj[k] = "[Error - cannot serialize]";
}
} else {
returnObj[k] = removeCircular(obj[k], refs);
returnObj[k] = "[Error - defined in the prototype but missing in the object]";
}
}
refs.pop();
refs.delete(obj);
return returnObj;
}

Expand Down
Loading