From 0179f792abba19893af0e8d2c2e79c9681636ca2 Mon Sep 17 00:00:00 2001 From: Mark Duckworth <1124037+MarkDuckworth@users.noreply.github.com> Date: Wed, 23 Jul 2025 16:58:12 -0600 Subject: [PATCH] Implement a safeStringify method that is the fallback if formatJson fails in the browser. --- .../src/platform/browser/format_json.ts | 26 ++++++++++++++++++- .../platform/browser/webchannel_connection.ts | 4 +-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/firestore/src/platform/browser/format_json.ts b/packages/firestore/src/platform/browser/format_json.ts index da0c4431f96..f3b4621f4ae 100644 --- a/packages/firestore/src/platform/browser/format_json.ts +++ b/packages/firestore/src/platform/browser/format_json.ts @@ -17,5 +17,29 @@ /** Formats an object as a JSON string, suitable for logging. */ export function formatJSON(value: unknown): string { - return JSON.stringify(value); + try { + return JSON.stringify(value); + } catch (e: unknown) { + return safeStringify(value); + } +} + +/** + * Custom JSON stringification utilizing a replacer to work around common + * JSON.stringify(...) error cases: circular reference or bigint. + * @param value - object to stringify + */ +function safeStringify(value: unknown): string { + const cache = new Set(); + return JSON.stringify(value, (key, value) => { + if (typeof value === 'object' && value !== null) { + if (cache.has(value)) { + return '[Circular]'; + } + cache.add(value); + } else if (typeof value === 'bigint') { + return value.toString(); + } + return value; + }); } diff --git a/packages/firestore/src/platform/browser/webchannel_connection.ts b/packages/firestore/src/platform/browser/webchannel_connection.ts index 56f57aa9595..e9ebdce5714 100644 --- a/packages/firestore/src/platform/browser/webchannel_connection.ts +++ b/packages/firestore/src/platform/browser/webchannel_connection.ts @@ -335,9 +335,7 @@ export class WebChannelConnection extends RestConnection { logWarn( LOG_TAG, `RPC '${rpcName}' stream ${streamId} transport errored. Name:`, - err.name, - 'Message:', - err.message + err ); streamBridge.callOnClose( new FirestoreError(