javascript - Global Error Handling In React Application
问题描述
I am having a medium sized React App using React-redux and other libs. in most circumstances i am handling the error the using Try catch and dispatching to my service, but every now and then i get a user which tries to access the page from mobile web with a old chrome / firefox / etc . I am not able to get the error stack for these from my system but when i simulate it i can get hold of the error in the console .
errors like
I am trying to write a global error handler for the same but it never seems to work . Things i have tried writing a ErrorBoundary and placing it where my redux provider is placed, but for unsupported systems like older firefox and chrome it fails even before this .
Tried writing a onerror function in the index.html and even in the app.js file but still the error is never caught.
export const ErrorHandler = () => {
window.addEventListener('unhandledrejection', function (e) {
alert('RAHUL'+ e.reason.messagea);// i want to send the error to backend here
});
window.onerror = function (msg: string, url: string, lineNo: number, columnNo: number, error: Error) {
const string = msg.toLowerCase();
const substring = "script error";
if (string.indexOf(substring) > -1) {
alert('RAHUL'+'Script Error: See Browser Console for Detail');// i want to send the error to backend here
} else {
const message = [
'Message: ' + msg,
'URL: ' + url,
'Line: ' + lineNo,
'Column: ' + columnNo,
'Error object: ' + JSON.stringify(error)
].join(' - ');
alert('RAHUL'+ message); // i want to send the error to backend here
}
return false;
};
};
I have looked at this question and others of the same type for reference.
Update
In our build system the React Assets are uploaded to a CDN while doing a build as we have a custom build solution and then inject to the jsp page which serves the assets , and as i was digging more into the error any external script error is not caught by window.onerror can be one of the solves , but for use cases like this how do we implement a global error handler
解决方案
推荐阅读
- ios - 如何知道我将从 Apple 应用内购买中收到的确切金额
- c# - 如何在 IIS 中使用的 Azure 中使用“wwwroot”文件夹
- c++ - .h 中定义的 constexpr 数据成员仅在 -std=c++14 及以下版本中未在 .cpp 中解析
- amazon-web-services - 某些位置的网站响应缓慢
- ruby-on-rails - PG::DatatypeMismatch:错误:WHERE 的参数必须是布尔类型,而不是整数类型
- c# - 使用 HttpClient 发布 JSON 数据时出现错误 400
- spring-boot - 如何在tomcat 9.0.14的spring boot REST应用程序中同时启用http1.1和http2.0
- php - 如果不在 PHP 中,登录尝试失败/限制延迟设置在哪里?
- python - 如何在 Internet Explorer 中使用 pywinauto 保存 html
- jhipster - Jhipster 使用 Maven 构建生产命令返回错误