javascript - 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
解决方案
推荐阅读
- java - 使用加速度传感器计算滚动角度
- css - 如何仅在某些锚点上应用 CSS
- xml - How to look for special/escape characters in XPATH 1.0
- java - 如何使用网络填充文件下载中的缓冲区 [android]
- java - Dockerized Spring Boot 微服务无故冻结
- json - jq,替换任何级别的空值,不触及非空或不存在
- regex - 正则表达式 - 完全匹配
- sorting - 如何检查切片是否已排序?
- javascript - 更新 Firestore 文档中的字段
- ios - UIBarButtonItem 图像大小不起作用