Skip to content

#1703: fixing logger runtime exceptions #1704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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