Skip to content
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

Remove circular references when stringifying an object #11

Merged
merged 5 commits into from
Sep 13, 2021
Merged
Changes from 4 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
111 changes: 42 additions & 69 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,138 +169,111 @@ const extension: JupyterFrontEndPlugin<void> = {
const _trace = console.trace;
const _table = console.table;

window.console.debug = (...args: any[]): void => {
// https://stackoverflow.com/a/11616993
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering whether we want to take this approach though.

As mentioned in the SA answer:

The replacer in this example is not 100% correct (depending on your definition of "duplicate"). In the following case, a value is discarded:

This indeed discards some of the elements:

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a warning message informing the user about the possibility of missing attributes

// We need to clear cache after each use.
let cache: any = [];
const refReplacer = (key: any, value: any) => {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
return;
}
cache.push(value);
}
return value;
};

const parseArgs = (args: any[]): string => {
let data = '';
args.forEach(arg => {
data +=
(typeof arg === 'object' && arg !== null
? JSON.stringify(arg)
: arg) + ' ';
try {
data +=
(typeof arg === 'object' && arg !== null
? JSON.stringify(arg)
: arg) + ' ';
} catch (e) {
try {
const msg =
'This error contains a object with a circular reference. During the process of removing the reference we could have removed duplicated attributes.\n';
jtpio marked this conversation as resolved.
Show resolved Hide resolved
const obj = JSON.stringify(arg, refReplacer);
cache = [];
console.error(msg, obj);
data += obj;
} catch (e) {
data += ' ';
}
}
});
return data;
};

window.console.debug = (...args: any[]): void => {
logConsolePanel?.logger?.log({
type: 'text',
level: 'debug',
data
data: parseArgs(args)
});
_debug(...args);
};

window.console.log = (...args: any[]): void => {
let data = '';
args.forEach(arg => {
data +=
(typeof arg === 'object' && arg !== null
? JSON.stringify(arg)
: arg) + ' ';
});

logConsolePanel?.logger?.log({
type: 'text',
level: 'debug',
data
data: parseArgs(args)
});
_log(...args);
};

window.console.info = (...args: any[]): void => {
let data = '';
args.forEach(arg => {
data +=
(typeof arg === 'object' && arg !== null
? JSON.stringify(arg)
: arg) + ' ';
});

logConsolePanel?.logger?.log({
type: 'text',
level: 'info',
data
data: parseArgs(args)
});
_info(...args);
};

window.console.warn = (...args: any[]): void => {
let data = '';
args.forEach(arg => {
data +=
(typeof arg === 'object' && arg !== null
? JSON.stringify(arg)
: arg) + ' ';
});

logConsolePanel?.logger?.log({
type: 'text',
level: 'warning',
data
data: parseArgs(args)
});
_warn(...args);
};

window.console.error = (...args: any[]): void => {
let data = '';
args.forEach(arg => {
data +=
(typeof arg === 'object' && arg !== null
? JSON.stringify(arg)
: arg) + ' ';
});

logConsolePanel?.logger?.log({
type: 'text',
level: 'critical',
data
data: parseArgs(args)
});
_error(...args);
};

window.console.exception = (message?: string, ...args: any[]): void => {
let data = '';
args.forEach(arg => {
data +=
(typeof arg === 'object' && arg !== null
? JSON.stringify(arg)
: arg) + ' ';
});

logConsolePanel?.logger?.log({
type: 'text',
level: 'critical',
data: `Exception: ${message}\n${data}`
data: `Exception: ${message}\n${parseArgs(args)}`
});
_exception(...args);
};

window.console.trace = (...args: any[]): void => {
let data = '';
args.forEach(arg => {
data +=
(typeof arg === 'object' && arg !== null
? JSON.stringify(arg)
: arg) + ' ';
});

logConsolePanel?.logger?.log({
type: 'text',
level: 'info',
data
data: parseArgs(args)
});
_trace(...args);
};

window.console.table = (...args: any[]): void => {
let data = '';
args.forEach(arg => {
data +=
(typeof arg === 'object' && arg !== null
? JSON.stringify(arg)
: arg) + ' ';
});

logConsolePanel?.logger?.log({
type: 'text',
level: 'info',
data
data: parseArgs(args)
});
_table(...args);
};
Expand Down