-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathautomaticErrorCollection.ts
More file actions
140 lines (123 loc) · 4.89 KB
/
automaticErrorCollection.ts
File metadata and controls
140 lines (123 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { FetchCompleteContext, resetFetchProxy, startFetchProxy } from '../browser/fetchProxy'
import { resetXhrProxy, startXhrProxy, XhrCompleteContext } from '../browser/xhrProxy'
import { ErrorSource, formatUnknownError, RawError, toStackTraceString } from '../tools/error'
import { Observable } from '../tools/observable'
import { jsonStringify, ONE_MINUTE, RequestType } from '../tools/utils'
import { Configuration } from './configuration'
import { monitor } from './internalMonitoring'
import { computeStackTrace, subscribe, unsubscribe, StackTrace } from './tracekit'
export type ErrorObservable = Observable<RawError>
let filteredErrorsObservable: ErrorObservable
export function startAutomaticErrorCollection(configuration: Configuration) {
if (!filteredErrorsObservable) {
const errorObservable = new Observable<RawError>()
trackNetworkError(configuration, errorObservable)
startConsoleTracking(errorObservable)
startRuntimeErrorTracking(errorObservable)
filteredErrorsObservable = filterErrors(configuration, errorObservable)
}
return filteredErrorsObservable
}
export function filterErrors(configuration: Configuration, errorObservable: Observable<RawError>) {
let errorCount = 0
const filteredErrorObservable = new Observable<RawError>()
errorObservable.subscribe((error: RawError) => {
if (errorCount < configuration.maxErrorsByMinute) {
errorCount += 1
filteredErrorObservable.notify(error)
} else if (errorCount === configuration.maxErrorsByMinute) {
errorCount += 1
filteredErrorObservable.notify({
message: `Reached max number of errors by minute: ${configuration.maxErrorsByMinute}`,
source: ErrorSource.AGENT,
startTime: performance.now(),
})
}
})
setInterval(() => (errorCount = 0), ONE_MINUTE)
return filteredErrorObservable
}
let originalConsoleError: (message?: any, ...optionalParams: any[]) => void
export function startConsoleTracking(errorObservable: ErrorObservable) {
originalConsoleError = console.error
console.error = monitor((message?: any, ...optionalParams: any[]) => {
originalConsoleError.apply(console, [message, ...optionalParams])
errorObservable.notify({
message: ['console error:', message, ...optionalParams].map(formatConsoleParameters).join(' '),
source: ErrorSource.CONSOLE,
startTime: performance.now(),
})
})
}
export function stopConsoleTracking() {
console.error = originalConsoleError
}
function formatConsoleParameters(param: unknown) {
if (typeof param === 'string') {
return param
}
if (param instanceof Error) {
return toStackTraceString(computeStackTrace(param))
}
return jsonStringify(param, undefined, 2)
}
let traceKitReportHandler: (stack: StackTrace, isWindowError: boolean, errorObject?: any) => void
export function startRuntimeErrorTracking(errorObservable: ErrorObservable) {
traceKitReportHandler = (stackTrace: StackTrace, _: boolean, errorObject?: any) => {
const { stack, message, type } = formatUnknownError(stackTrace, errorObject, 'Uncaught')
errorObservable.notify({
message,
stack,
type,
source: ErrorSource.SOURCE,
startTime: performance.now(),
})
}
subscribe(traceKitReportHandler)
}
export function stopRuntimeErrorTracking() {
unsubscribe(traceKitReportHandler)
}
export function trackNetworkError(configuration: Configuration, errorObservable: ErrorObservable) {
startXhrProxy().onRequestComplete((context) => handleCompleteRequest(RequestType.XHR, context))
startFetchProxy().onRequestComplete((context) => handleCompleteRequest(RequestType.FETCH, context))
function handleCompleteRequest(type: RequestType, request: XhrCompleteContext | FetchCompleteContext) {
if (!configuration.isIntakeUrl(request.url) && (isRejected(request) || isServerError(request))) {
errorObservable.notify({
message: `${format(type)} error ${request.method} ${request.url}`,
resource: {
method: request.method,
statusCode: request.status,
url: request.url,
},
source: ErrorSource.NETWORK,
stack: truncateResponse(request.response, configuration) || 'Failed to load',
startTime: request.startTime,
})
}
}
return {
stop: () => {
resetXhrProxy()
resetFetchProxy()
},
}
}
function isRejected(request: { status: number; responseType?: string }) {
return request.status === 0 && request.responseType !== 'opaque'
}
function isServerError(request: { status: number }) {
return request.status >= 500
}
function truncateResponse(response: string | undefined, configuration: Configuration) {
if (response && response.length > configuration.requestErrorResponseLengthLimit) {
return `${response.substring(0, configuration.requestErrorResponseLengthLimit)}...`
}
return response
}
function format(type: RequestType) {
if (RequestType.XHR === type) {
return 'XHR'
}
return 'Fetch'
}