Skip to content

Commit 539639d

Browse files
bitekastaeold
andauthored
#1703: fixing logger runtime exceptions (#1704)
* #608: fixing logger runtime exceptions * Fixing linter errors * add changelog. --------- Co-authored-by: Daniel Lee <danielylee@google.com> Co-authored-by: Daniel Lee <taeold@gmail.com>
1 parent 17e71b7 commit 539639d

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
- Add LLM guidance (#1736)
2+
- Fix logger runtime exceptions #(1704)

src/logger/index.ts

Lines changed: 16 additions & 8 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,21 @@ 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]";
75+
if (obj.hasOwnProperty(k)) {
76+
try {
77+
if (refs.has(obj[k])) {
78+
returnObj[k] = "[Circular]";
79+
} else {
80+
returnObj[k] = removeCircular(obj[k], refs);
81+
}
82+
} catch {
83+
returnObj[k] = "[Error - cannot serialize]";
84+
}
7785
} else {
78-
returnObj[k] = removeCircular(obj[k], refs);
86+
returnObj[k] = "[Error - defined in the prototype but missing in the object]";
7987
}
8088
}
81-
refs.pop();
89+
refs.delete(obj);
8290
return returnObj;
8391
}
8492

0 commit comments

Comments
 (0)