-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCrashReporter.ts
81 lines (69 loc) · 2.66 KB
/
CrashReporter.ts
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
import { type AttributeType, type BacktraceAttachment, type FileSystem } from '@backtrace/sdk-core';
import { NativeModules } from 'react-native';
import { BacktraceFileAttachment } from '../attachment/BacktraceFileAttachment';
import { DebuggerHelper } from '../common/DebuggerHelper';
export class CrashReporter {
private static readonly BacktraceReactNative = NativeModules.BacktraceReactNative;
private _enabled = false;
constructor(private readonly _fileSystem: FileSystem) {}
/**
* Determines if the crash reporting solution was already initialized.
*/
private static initialized = false;
public initialize(
submissionUrl: string,
databasePath: string,
attributes: Record<string, AttributeType>,
attachments: readonly BacktraceAttachment[],
): boolean {
if (CrashReporter.initialized) {
return false;
}
// verify if the native bindings are available
if (!CrashReporter.BacktraceReactNative) {
return false;
}
if (!DebuggerHelper.isNativeBridgeEnabled()) {
return false;
}
const nativeDatabasePath = `${databasePath}/native`;
this._fileSystem.createDirSync(nativeDatabasePath);
CrashReporter.BacktraceReactNative.initialize(
submissionUrl,
nativeDatabasePath,
{
...this.convertAttributes(attributes),
'error.type': 'Crash',
},
attachments
.filter((n) => n instanceof BacktraceFileAttachment)
.map((n) => (n as BacktraceFileAttachment).filePath),
);
this._enabled = true;
CrashReporter.initialized = true;
return true;
}
public updateAttributes(attributes: Record<string, AttributeType>) {
if (!this._enabled) {
return;
}
CrashReporter.BacktraceReactNative.useAttributes(this.convertAttributes(attributes));
}
public static crash(): void {
if (CrashReporter.BacktraceReactNative) {
CrashReporter.BacktraceReactNative.crash();
} else {
throw new Error('Native binding is not available');
}
}
public dispose(): void {
this._enabled = false;
}
/**
* Native layer might not support fully all types supported by the JavaScript SDK. The method converts attributes
* to model fully supported by the native env
*/
private convertAttributes(attributes: Record<string, AttributeType>): Record<string, string> {
return Object.fromEntries(Object.entries(attributes).map(([key, value]) => [key, value?.toString() ?? '']));
}
}