Skip to content
Open
Show file tree
Hide file tree
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
26 changes: 25 additions & 1 deletion packages/firestore/src/platform/browser/format_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading