Skip to content

Commit 5a3de73

Browse files
bitekascabljac
authored andcommitted
#608: fixing logger runtime exceptions
1 parent 534505c commit 5a3de73

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/logger/index.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ export interface LogEntry {
5252
}
5353

5454
/** @internal */
55-
function removeCircular(obj: any, refs: any[] = []): any {
55+
function removeCircular(obj: any, refs: Set<any> = new Set()): any {
5656
if (typeof obj !== "object" || !obj) {
5757
return obj;
5858
}
5959
// If the object defines its own toJSON, prefer that.
60-
if (obj.toJSON) {
60+
if (obj.toJSON && typeof obj.toJSON === "function") {
6161
return obj.toJSON();
6262
}
63-
if (refs.includes(obj)) {
63+
if (refs.has(obj)) {
6464
return "[Circular]";
6565
} else {
66-
refs.push(obj);
66+
refs.add(obj);
6767
}
6868
let returnObj: any;
6969
if (Array.isArray(obj)) {
@@ -72,13 +72,17 @@ function removeCircular(obj: any, refs: any[] = []): any {
7272
returnObj = {};
7373
}
7474
for (const k in obj) {
75-
if (refs.includes(obj[k])) {
76-
returnObj[k] = "[Circular]";
77-
} else {
78-
returnObj[k] = removeCircular(obj[k], refs);
75+
try {
76+
if (refs.has(obj[k])) {
77+
returnObj[k] = "[Circular]";
78+
} else {
79+
returnObj[k] = removeCircular(obj[k], refs);
80+
}
81+
} catch {
82+
returnObj[k] = "[Error - cannot serialize]";
7983
}
8084
}
81-
refs.pop();
85+
refs.delete(obj);
8286
return returnObj;
8387
}
8488

0 commit comments

Comments
 (0)