rrweb: replace onerror with addEventListener('error',handler) in the rrweb console plugin (#761)

This commit is contained in:
Lucky Feng
2021-11-30 13:18:57 +08:00
committed by GitHub
parent 1f267082cb
commit f75679015d

View File

@@ -124,29 +124,21 @@ function initLogObserver(
// add listener to thrown errors // add listener to thrown errors
if (logOptions.level!.includes('error')) { if (logOptions.level!.includes('error')) {
if (window) { if (window) {
const originalOnError = window.onerror; const errorHandler = (event: ErrorEvent) => {
window.onerror = ( const { message, error } = event;
msg: Event | string,
file: string,
line: number,
col: number,
error: Error,
) => {
if (originalOnError) {
originalOnError.apply(this, [msg, file, line, col, error]);
}
const trace: string[] = ErrorStackParser.parse( const trace: string[] = ErrorStackParser.parse(
error, error,
).map((stackFrame: StackFrame) => stackFrame.toString()); ).map((stackFrame: StackFrame) => stackFrame.toString());
const payload = [stringify(msg, logOptions.stringifyOptions)]; const payload = [stringify(message, logOptions.stringifyOptions)];
cb({ cb({
level: 'error', level: 'error',
trace, trace,
payload, payload,
}); });
}; };
window.addEventListener('error', errorHandler);
cancelHandlers.push(() => { cancelHandlers.push(() => {
window.onerror = originalOnError; if (window) window.removeEventListener('error', errorHandler);
}); });
} }
} }