Skip to content

Commit

Permalink
rrweb: replace onerror with addEventListener('error',handler) in the …
Browse files Browse the repository at this point in the history
…rrweb console plugin (#761)
  • Loading branch information
YunFeng0817 authored Nov 30, 2021
1 parent 1f26708 commit f756790
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions packages/rrweb/src/plugins/console/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,29 +124,21 @@ function initLogObserver(
// add listener to thrown errors
if (logOptions.level!.includes('error')) {
if (window) {
const originalOnError = window.onerror;
window.onerror = (
msg: Event | string,
file: string,
line: number,
col: number,
error: Error,
) => {
if (originalOnError) {
originalOnError.apply(this, [msg, file, line, col, error]);
}
const errorHandler = (event: ErrorEvent) => {
const { message, error } = event;
const trace: string[] = ErrorStackParser.parse(
error,
).map((stackFrame: StackFrame) => stackFrame.toString());
const payload = [stringify(msg, logOptions.stringifyOptions)];
const payload = [stringify(message, logOptions.stringifyOptions)];
cb({
level: 'error',
trace,
payload,
});
};
window.addEventListener('error', errorHandler);
cancelHandlers.push(() => {
window.onerror = originalOnError;
if (window) window.removeEventListener('error', errorHandler);
});
}
}
Expand Down

0 comments on commit f756790

Please sign in to comment.