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

This commit is contained in:
Lucky Feng
2026-04-01 12:00:00 +08:00
committed by GitHub
parent bcf42a7ff0
commit d9333f675b

View File

@@ -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);
});
}
}