Skip to content

Commit c51277a

Browse files
authored
Merge pull request #8 from EzStars/docs/Zero
docs: 📝 增加错误监控相关文档
2 parents 5fe5c8b + b5e00d3 commit c51277a

5 files changed

Lines changed: 225 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## js错误捕获
2+
3+
```
4+
window.addEventListener(
5+
'error',
6+
(e: ErrorEvent | Event) => {
7+
const errorType = getErrorType(e)
8+
switch (errorType) {
9+
case TraceSubTypeEnum.resource:
10+
initResourceError(e)
11+
break
12+
case TraceSubTypeEnum.js:
13+
initJsError(e as ErrorEvent)
14+
break
15+
case TraceSubTypeEnum.cors:
16+
initCorsError(e as ErrorEvent)
17+
break
18+
default:
19+
break
20+
}
21+
},
22+
true
23+
)
24+
25+
```
26+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## promise错误捕获
2+
promise的错误是通过监听unhandledrejection事件捕获的,但捕获到的错误是无法获取到错误文件还是错误行列数的
3+
4+
```
5+
window.addEventListener(
6+
'unhandledrejection',
7+
(e: PromiseRejectionEvent) => {
8+
const stack = parseStackFrames(e.reason)
9+
const behavior = getBehaviour()
10+
const state = behavior?.breadcrumbs?.state || []
11+
const eventData = getRecordScreenData()
12+
const reportData: PromiseErrorType = {
13+
type: TraceTypeEnum.error,
14+
subType: TraceSubTypeEnum.promise,
15+
message: e.reason.message || e.reason,
16+
stack,
17+
pageUrl: window.location.href,
18+
errId: getErrorUid(`'promise'-${e.reason.message}`),
19+
state,
20+
timestamp: new Date().getTime(),
21+
eventData
22+
}
23+
// todo 发送错误信息
24+
lazyReportBatch(reportData)
25+
},
26+
true
27+
)
28+
29+
```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
## react错误捕获
2+
React16开始,官方提供ErrorBoundary错误边界,被该组件包裹的子组件render函数报错时会触发离当前组件最近的父组件ErrorBoundary
3+
4+
这种情况下,可以通过componentDidCatch将捕获的错误上报
5+
6+
```
7+
import React, { ReactNode } from 'react'
8+
import { lazyReportBatch } from '../common/report'
9+
import {
10+
getErrorUid,
11+
getReactComponentInfo,
12+
parseStackFrames
13+
} from '../common/utils'
14+
import { ReactErrorType } from '../types'
15+
import { TraceSubTypeEnum, TraceTypeEnum } from '../common/enum'
16+
import { getBehaviour, getRecordScreenData } from '../behavior'
17+
18+
interface ErrorBoundaryProps {
19+
Fallback: ReactNode // ReactNode 表示任意有效的 React 内容
20+
children: ReactNode
21+
}
22+
23+
interface ErrorBoundaryState {
24+
hasError: boolean
25+
}
26+
27+
let err = {}
28+
29+
class ErrorBoundary extends React.Component<
30+
ErrorBoundaryProps,
31+
ErrorBoundaryState
32+
> {
33+
state: ErrorBoundaryState = { hasError: false }
34+
35+
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
36+
this.setState({ hasError: true })
37+
const { componentName, url: src } = getReactComponentInfo(errorInfo)
38+
const type = TraceTypeEnum.error
39+
const subType = TraceSubTypeEnum.react
40+
const message = error.message
41+
const stack = parseStackFrames(error)
42+
const pageUrl = window.location.href
43+
const errId = getErrorUid(`${subType}-${message}-${src}`)
44+
const info = error.message
45+
const behavior = getBehaviour()
46+
const state = behavior?.breadcrumbs?.state || []
47+
const eventData = getRecordScreenData()
48+
const reportData: ReactErrorType = {
49+
type,
50+
subType,
51+
stack,
52+
pageUrl,
53+
message,
54+
errId,
55+
componentName,
56+
info,
57+
src,
58+
state,
59+
timestamp: new Date().getTime(),
60+
eventData
61+
}
62+
err = reportData
63+
lazyReportBatch(reportData)
64+
}
65+
66+
render() {
67+
const { Fallback } = this.props
68+
if (this.state.hasError) {
69+
// @ts-ignore
70+
return <Fallback error={err}/>
71+
}
72+
73+
return this.props.children
74+
}
75+
}
76+
77+
export default ErrorBoundary
78+
79+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## vue错误捕获
2+
`errorHandler``Vue` 提供的全局错误处理方法,用于捕获组件渲染、事件处理、生命周期钩子和子组件中的未捕获错误。开发者可以通过 `errorHandler` 实现错误的统一处理和上报。
3+
4+
```
5+
import { getBehaviour, getRecordScreenData } from '../behavior'
6+
import { TraceSubTypeEnum, TraceTypeEnum } from '../common/enum'
7+
import { lazyReportBatch } from '../common/report'
8+
import {
9+
getErrorUid,
10+
getVueComponentInfo,
11+
parseStackFrames
12+
} from '../common/utils'
13+
import { VueErrorType } from '../types'
14+
15+
// 初始化 Vue异常 的数据获取和上报
16+
export interface Vue {
17+
config: {
18+
errorHandler?: any
19+
warnHandler?: any
20+
}
21+
}
22+
23+
const initVueError = (app: Vue) => {
24+
app.config.errorHandler = (err: Error, vm: any, info: string) => {
25+
console.error(err)
26+
const { componentName, url: src } = getVueComponentInfo(vm)
27+
const type = TraceTypeEnum.error
28+
const subType = TraceSubTypeEnum.vue
29+
const message = err.message
30+
const stack = parseStackFrames(err)
31+
const pageUrl = window.location.href
32+
const behavior = getBehaviour()
33+
const state = behavior?.breadcrumbs?.state || []
34+
const eventData = getRecordScreenData()
35+
const reportData: VueErrorType = {
36+
type,
37+
subType,
38+
message,
39+
stack,
40+
pageUrl,
41+
info,
42+
componentName,
43+
src,
44+
errId: getErrorUid(`${subType}-${message}-${src}`),
45+
state,
46+
timestamp: new Date().getTime(),
47+
eventData
48+
}
49+
lazyReportBatch(reportData)
50+
}
51+
}
52+
53+
export default initVueError
54+
55+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## 资源加载错误捕获
2+
3+
收集 JavaScript、CSS 和图片等加载错误
4+
5+
```
6+
const initResourceError = (e: Event) => {
7+
// 通过 e.target 确定错误是发生在哪个资源上
8+
const target = e.target as ResourceErrorTarget
9+
// img是src,link就是href
10+
const src = target.src || target.href
11+
const type = e.type
12+
const subType = TraceSubTypeEnum.resource
13+
const tagName = target.tagName
14+
const message = ''
15+
const html = target.outerHTML
16+
// 获取dom加载位置
17+
const path = getPathToElement(target)
18+
const behavior = getBehaviour()
19+
const state = behavior?.breadcrumbs?.state || []
20+
const reportData: ResourceErrorType = {
21+
type,
22+
subType,
23+
tagName,
24+
message,
25+
html,
26+
src,
27+
pageUrl: window.location.href,
28+
path,
29+
errId: getErrorUid(`${subType}-${message}-${src}`),
30+
state,
31+
timestamp: new Date().getTime()
32+
}
33+
lazyReportBatch(reportData)
34+
}
35+
36+
```

0 commit comments

Comments
 (0)