Skip to content

Commit

Permalink
change the window object to the passed in 'win'
Browse files Browse the repository at this point in the history
  • Loading branch information
YunFeng0817 committed Feb 2, 2023
1 parent 50b930e commit 01165e8
Showing 1 changed file with 34 additions and 40 deletions.
74 changes: 34 additions & 40 deletions packages/rrweb/src/plugins/console/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,48 +114,42 @@ function initLogObserver(
const cancelHandlers: listenerHandler[] = [];
// add listener to thrown errors
if (logOptions.level.includes('error')) {
if (window) {
const errorHandler = (event: ErrorEvent) => {
const message = event.message,
error = event.error as Error;
const trace: string[] = ErrorStackParser.parse(
error,
).map((stackFrame: StackFrame) => stackFrame.toString());
const payload = [stringify(message, logOptions.stringifyOptions)];
cb({
level: 'error',
trace,
payload,
});
};
window.addEventListener('error', errorHandler);
cancelHandlers.push(() => {
if (window) window.removeEventListener('error', errorHandler);
const errorHandler = (event: ErrorEvent) => {
const message = event.message,
error = event.error as Error;
const trace: string[] = ErrorStackParser.parse(
error,
).map((stackFrame: StackFrame) => stackFrame.toString());
const payload = [stringify(message, logOptions.stringifyOptions)];
cb({
level: 'error',
trace,
payload,
});
const unhandledrejectionHandler = (event: PromiseRejectionEvent) => {
const error =
event.reason instanceof Error
? event.reason
: new Error(`Uncaught (in promise) ${event.reason as string}`);
const trace: string[] = ErrorStackParser.parse(
error,
).map((stackFrame: StackFrame) => stackFrame.toString());
const payload = [stringify(error.message, logOptions.stringifyOptions)];
cb({
level: 'error',
trace,
payload,
});
};
window.addEventListener('unhandledrejection', unhandledrejectionHandler);
cancelHandlers.push(() => {
if (window)
window.removeEventListener(
'unhandledrejection',
unhandledrejectionHandler,
);
};
win.addEventListener('error', errorHandler);
cancelHandlers.push(() => {
win.removeEventListener('error', errorHandler);
});
const unhandledrejectionHandler = (event: PromiseRejectionEvent) => {
const error =
event.reason instanceof Error
? event.reason
: new Error(`Uncaught (in promise) ${event.reason as string}`);
const trace: string[] = ErrorStackParser.parse(
error,
).map((stackFrame: StackFrame) => stackFrame.toString());
const payload = [stringify(error.message, logOptions.stringifyOptions)];
cb({
level: 'error',
trace,
payload,
});
}
};
win.addEventListener('unhandledrejection', unhandledrejectionHandler);
cancelHandlers.push(() => {
win.removeEventListener('unhandledrejection', unhandledrejectionHandler);
});
}
for (const levelType of logOptions.level) {
cancelHandlers.push(replace(logger, levelType));
Expand Down

0 comments on commit 01165e8

Please sign in to comment.