首页 > 解决方案 > window.onerror / window.addEventListener 在我的生产环境中没有被触发

问题描述

我尝试像在 main.tsx 文件中一样添加 window.onError 和 window.addEventListener

try {
  window.onerror = (message: string, source: string, lineno: number, colno: number, error: Error) => {
    console.log("reached inside the window.onerror");
    console.log(message);
  };
} catch (e) {
  console.log("reached the catch block");
  const message = getErrorMessage(e);
  logErrors(ClickEvent.GLOBAL_ERROR_LOG, message, ErrorEventLog.FATAL, pageId);
}

以类似的方式,我尝试了 window.addEventListener

window.addEventListener('error', event => {
 console.log('insideventlistener');
 console.log(event);
});
window.addEventListener('unhandledrejection', event => {
 console.log('unhandledrejection');
 return logClientSideError(event.reason);
});

注入的错误看起来像这样:

render() {
  const sample = null;
  return <div>{sample.abc}</div>;
}

问题:

在本地/开发环境中 - window.onerror 或 window.addEventListener 被触发,并且 console.log 语句与错误一起显示在浏览器控制台中。

但是,当我将代码推送到生产环境时,它们永远不会被触发。(但错误显示在浏览器控制台中)我错过了什么吗?

我们在部分代码中也添加了错误边界。

Prod 中的浏览器控制台出错

react_devtools_backend.js:2273 TypeError: Cannot read property 'as' of null
    at t.render (main.js:8)
    at un (main.js:37)
    at sn (main.js:37)
    at Bu (main.js:45)
    at ta (main.js:37)
    at $n (main.js:37)
    at Wn (main.js:37)
    at main.js:37
    at t.unstable_runWithPriority (main.js:69)
    at Wt (main.js:37)
    at Qt (main.js:37)
    at Kt (main.js:37)
    at Fn (main.js:37)
    at Object.enqueueSetState (main.js:45)
    at o.a.setState (main.js:61)
    at t (main.js:2)
    at main.js:2

标签: javascriptreactjsclientdom-events

解决方案


推荐阅读