Skip to content

fix: Improve logging of errors in the browser if a circular reference or bigint is encountered #9181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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