Skip to content

Commit ed1ef3a

Browse files
authored
chore: stringify objects logged in the background worker (#1097)
1 parent 28d6e01 commit ed1ef3a

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

background-charm-service/src/worker.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ onConsole(
7171
}
7272
}
7373
ctx = ctx ?? "Charm(NO_CHARM)";
74-
return [ctx, ...args];
74+
return [ctx, ...fmtConsole(args)];
7575
},
7676
);
7777

@@ -190,6 +190,22 @@ async function runCharm(data: RunData): Promise<void> {
190190
}
191191
}
192192

193+
// Logs here are often viewed through observability dashboards
194+
// that don't render objects well. Attempt to stringify any objects
195+
// here.
196+
function fmtConsole(args: any[]): any[] {
197+
return [...args].map((value) => {
198+
if (value && typeof value === "object") {
199+
try {
200+
return JSON.stringify(value);
201+
} catch (_e) {
202+
// satisfy typescript's empty block
203+
}
204+
}
205+
return value;
206+
});
207+
}
208+
193209
self.addEventListener("unhandledrejection", (e: PromiseRejectionEvent) => {
194210
// Throw this so that `WorkerController`'s `error` handler can handle
195211
// unhandled rejections the same way unhandled errors are handled.

0 commit comments

Comments
 (0)