背景
- 当访问了空对象的属性、空函数调用等情况时,会引起前端错误,导致白屏,影响用户体验,主要有以下两种:
- 前端代码错误:可以使用typescript静态属性检查,排除错误。
- 后端的接口未按照约定返回数据。
- 栈溢出。
目标
- 对于平台错误,进行全局和组件级别的拦截,达到在白屏的情况下,至少一部分是可见的,引导用户联系平台负责人,提高平台稳定性,增强用户体验。
- 对于白屏错误进行监控和上报,上报信息:页面,错误信息,时间等。
react错误边界只能捕获:
- 子组件的渲染错误
- 生命周期函数内的错误
- 构造函数内的错误
不可以捕获:
- 事件处理函数内的错误(如onClick函数中的错误)
- 异步代码中的错误(如setTimeout回调函数中的错误)
- 服务端渲染
- 错误边界代码自身的错误
方案一
封装高阶函数
import React, { ErrorInfo } from 'react';
interface IState {
hasError: boolean;
error: Error | null;
errorInfo: ErrorInfo | null;
}
export function withErrorHandler(Component, ErrorComp?) {
class WithErrorHandler extends React.Component<any, IState> {
constructor(props) {
super(props);
this.state = {
hasError: false,
// 错误信息,是否需要展示出来呢?
error: null,
errorInfo: null,
};
}
public componentDidCatch(error, info) {
this.setState({ hasError: true, error, errorInfo: info });
// 监控上报错误
}
public render() {
if (this.state.hasError) {
return (
ErrorComp || <div style={{padding: '20px'}}>出现了严重错误,无法渲染</div>
);
}
return <Component {...this.props} />;
}
}
WithErrorHandler.displayName = `withErrorHandler(${Component.displayName})`;
return WithErrorHandler;
}
在组件中使用
通过webpack处理所有React.component的导出,将导出的组件,用高阶组件包裹。juejin.cn/post/698504…
在Aview组件里只能解决一部分问题,个别组件无法解决。全站组件量大,无法保证稳定性,后面人员接入可能需要有一定的了解。
方案二
使用现有插件 github.com/gaearon/rea…
使用插件react-transform-catch-errors。
安装 babel-plugin-react-transform 和 react-transform-catch-errors
配置.babelrc参考如下:
{
"presets": ["es2015", "stage-0"],
"env": {
// only enable it when process.env.NODE_ENV is 'development' or undefined
"development": {
"plugins": [["react-transform", {
"transforms": [{
"transform": "react-transform-catch-errors",
// now go the imports!
"imports": [
// the first import is your React distribution
// (if you use React Native, pass "react-native" instead)
"react",
// the second import is the React component to render error
// (it can be a local path too, like "./src/ErrorReporter")
"./src/ErrorReporter"
// the third import is OPTIONAL!
// when specified, its export is used as options to the reporter.
// see specific reporter's docs for the options it needs.
// it will be imported from different files so it either has to be a Node module
// or a file that you configure with Webpack/Browserify/SystemJS to resolve correctly.
// for example, see https://github.com/gaearon/babel-plugin-react-transform/pull/28#issuecomment-144536185
// , "my-reporter-options"
]
}]
// note: you can put more transforms into array
// this is just one of them!
}]]
}
}
}
但是此插件没有维护了,且只能在开发环境中使用,且并没有被很多项目使用(周下载量很少,见www.npmjs.com/package/rea…),可以自己封装一个webpack插件在线上使用。参考插件源码
此方案是在componentDidCatch生命周期出现之前。
方案三
自定义React.createElement
写一个错误组件,重写React.createElement。使用 babel-plugin-transform-react-jsx 来更改 jsx 转化逻辑,将 jsx 用到的 createElement 替换成自己写的createElement方法。
举例子:imcuttle.github.io/%E8%87%AA%E…
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
componentDidCatch(error, info) {
// Display fallback UI
this.setState({ hasError: true });
// You can also log the error to an error reporting service
logErrorToMyService(error, info);
}
render() {
if (this.state.hasError) {
// You can render any custom fallback UI
return <h1>Something went wrong.</h1>;
}
return this.props.children;
}
}
const rc = React.createElement
React.createElement = (type, config, ...other) => {
return rc(ErrorBoundary, null, rc(type, config, ...other))
}
不需要他人接入。
相关参考链接:
react.docschina.org/docs/higher…
www.npmjs.com/package/bab…
方案对比
方案 | 优点 | 缺点 |
---|---|---|
方案一:高阶组件 | 是大多数错误捕获思路,易于理解。 | 1、全局替换成本高,回测范围广泛。2、新手接入不友好,每一个新人都必须要知道这个规则。3、不能解决继承React.Component的组件 |
方案二:插件 | 对于低版本的React而已相对简单,开发者不需要自己更改源码 | 支持的React版本低,不适合项目 |
方案三:自定义React.createElement | 仅由一个人即可维护,不要其他开发者考虑和介入 | 直接显示使用 React.createElment 或者 React.createFactory 的地方则不能涵盖到。 |
错误上报
公司内部埋点工具。