-
Notifications
You must be signed in to change notification settings - Fork 45
/
DdRumErrorTracking.tsx
163 lines (144 loc) · 5.24 KB
/
DdRumErrorTracking.tsx
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/
import type { ErrorHandlerCallback } from 'react-native';
import { InternalLog } from '../../InternalLog';
import { SdkVerbosity } from '../../SdkVerbosity';
import { DdLogs } from '../../logs/DdLogs';
import {
getErrorMessage,
getErrorStackTrace,
EMPTY_STACK_TRACE,
getErrorName,
DEFAULT_ERROR_NAME
} from '../../utils/errorUtils';
import { executeWithDelay } from '../../utils/jsUtils';
import { DdRum } from '../DdRum';
import { ErrorSource } from '../types';
/**
* Provides RUM auto-instrumentation feature to track errors as RUM events.
*/
export class DdRumErrorTracking {
private static isTracking = false;
private static isInDefaultErrorHandler = false;
// eslint-disable-next-line
private static defaultErrorHandler: ErrorHandlerCallback = (_error: any, _isFatal?: boolean) => { }
// eslint-disable-next-line
private static defaultConsoleError = (..._params: unknown[]) => { }
/**
* Starts tracking errors and sends a RUM Error event every time an error is detected.
*/
static startTracking(): void {
// extra safety to avoid wrapping the Error handler twice
if (DdRumErrorTracking.isTracking) {
InternalLog.log(
'Datadog SDK is already tracking errors',
SdkVerbosity.WARN
);
return;
}
if (ErrorUtils) {
DdRumErrorTracking.defaultErrorHandler = ErrorUtils.getGlobalHandler();
DdRumErrorTracking.defaultConsoleError = console.error;
ErrorUtils.setGlobalHandler(DdRumErrorTracking.onGlobalError);
console.error = DdRumErrorTracking.onConsoleError;
DdRumErrorTracking.isTracking = true;
InternalLog.log(
'Datadog SDK is tracking errors',
SdkVerbosity.INFO
);
} else {
InternalLog.log(
'Datadog SDK cannot track errors, ErrorUtils is not defined',
SdkVerbosity.ERROR
);
}
}
static onGlobalError = (error: any, isFatal?: boolean): void => {
const message = getErrorMessage(error);
const stacktrace = getErrorStackTrace(error);
const errorName = getErrorName(error);
this.reportError(message, ErrorSource.SOURCE, stacktrace, errorName, {
'_dd.error.is_crash': isFatal,
'_dd.error.raw': error
}).then(async () => {
DdRumErrorTracking.isInDefaultErrorHandler = true;
try {
// On real iOS devices, the crash context is not updated soon
// enough for the view update to contain the crash.
// Waiting for 50ms has low impact and ensures the crash context
// is updated before actually crashing the app.
await executeWithDelay(
() =>
DdRumErrorTracking.defaultErrorHandler(error, isFatal),
50
);
} finally {
DdRumErrorTracking.isInDefaultErrorHandler = false;
}
});
};
static onConsoleError = (...params: unknown[]): void => {
if (DdRumErrorTracking.isInDefaultErrorHandler) {
return;
}
let stack: string = EMPTY_STACK_TRACE;
let errorName: string = DEFAULT_ERROR_NAME;
for (let i = 0; i < params.length; i += 1) {
const param = params[i];
const paramStack = getErrorStackTrace(param);
if (paramStack !== EMPTY_STACK_TRACE) {
stack = paramStack;
}
const paramErrorName = getErrorName(param);
if (paramErrorName !== DEFAULT_ERROR_NAME) {
errorName = paramErrorName;
}
if (
errorName !== DEFAULT_ERROR_NAME &&
stack !== EMPTY_STACK_TRACE
) {
break;
}
}
const message = params
.map(param => {
if (typeof param === 'string') {
return param;
} else {
return getErrorMessage(param);
}
})
.join(' ');
this.reportError(message, ErrorSource.CONSOLE, stack, errorName).then(
() => {
DdRumErrorTracking.defaultConsoleError.apply(console, params);
}
);
};
private static reportError = (
message: string,
source: ErrorSource,
stacktrace: string,
errorName: string,
context: object = {}
): Promise<[void, void]> => {
return Promise.all([
DdRum.addError(message, source, stacktrace, context),
DdLogs.error(
message,
errorName,
message,
stacktrace,
{
...context,
'_dd.error_log.is_crash': true
},
undefined,
source
)
]);
};
}