-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBacktraceClient.ts
166 lines (147 loc) · 5.87 KB
/
BacktraceClient.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
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
164
165
166
import {
BacktraceCoreClient,
BreadcrumbsManager,
SingleSessionProvider,
SubmissionUrlInformation,
V8StackTraceConverter,
VariableDebugIdMapProvider,
type AttributeType,
type DebugIdContainer,
} from '@backtrace/sdk-core';
import { NativeModules, Platform } from 'react-native';
import { type BacktraceConfiguration } from './BacktraceConfiguration';
import { FileBreadcrumbsStorage } from './breadcrumbs/FileBreadcrumbsStorage';
import { BacktraceClientBuilder } from './builder/BacktraceClientBuilder';
import type { BacktraceClientSetup } from './builder/BacktraceClientSetup';
import { version } from './common/platformHelper';
import { CrashReporter } from './crashReporter/CrashReporter';
import { generateUnhandledExceptionHandler } from './handlers';
import { type ExceptionHandler } from './handlers/ExceptionHandler';
import { ReactNativeRequestHandler } from './ReactNativeRequestHandler';
import { ReactStackTraceConverter } from './ReactStackTraceConverter';
import { type FileSystem } from './storage/FileSystem';
export class BacktraceClient extends BacktraceCoreClient<BacktraceConfiguration> {
private readonly _crashReporter?: CrashReporter;
private readonly _exceptionHandler: ExceptionHandler = generateUnhandledExceptionHandler();
public crash(): void {
CrashReporter.crash();
}
public static get applicationDataPath(): string {
return NativeModules.BacktraceDirectoryProvider?.applicationDirectory() ?? '';
}
constructor(clientSetup: BacktraceClientSetup) {
super({
sdkOptions: {
agent: '@backtrace/react-native',
agentVersion: '0.0.1',
langName: 'react-native',
langVersion: version(),
},
requestHandler: new ReactNativeRequestHandler(clientSetup.options),
debugIdMapProvider: new VariableDebugIdMapProvider(global as DebugIdContainer),
stackTraceConverter: new ReactStackTraceConverter(new V8StackTraceConverter('address at')),
sessionProvider: new SingleSessionProvider(),
...clientSetup,
});
const fileSystem = clientSetup.fileSystem as FileSystem;
if (!fileSystem) {
return;
}
const breadcrumbsManager = this.modules.get(BreadcrumbsManager);
if (breadcrumbsManager && this.sessionFiles) {
breadcrumbsManager.setStorage(
FileBreadcrumbsStorage.create(
fileSystem,
this.sessionFiles,
(clientSetup.options.breadcrumbs?.maximumBreadcrumbs ?? 100) || 100,
),
);
}
this.attributeManager.attributeEvents.on(
'scoped-attributes-updated',
(reportData: { attributes: Record<string, AttributeType> }) => {
this._crashReporter?.updateAttributes(reportData.attributes);
},
);
}
public initialize(): void {
const lockId = this.sessionFiles?.lockPreviousSessions();
try {
super.initialize();
this.captureUnhandledErrors(
this.options.captureUnhandledErrors,
this.options.captureUnhandledPromiseRejections,
);
this.initializeNativeCrashReporter();
} finally {
lockId && this.sessionFiles?.unlockPreviousSessions(lockId);
}
}
public dispose(): void {
this._exceptionHandler.dispose();
this._crashReporter?.dispose();
super.dispose();
}
public static builder(options: BacktraceConfiguration): BacktraceClientBuilder {
return new BacktraceClientBuilder({ options });
}
/**
* Initializes the client. If the client already exists, the available instance
* will be returned and all other options will be ignored.
* @param options client configuration
* @param build builder
* @returns backtrace client
*/
public static initialize(
options: BacktraceConfiguration,
build?: (builder: BacktraceClientBuilder) => void,
): BacktraceClient {
if (this.instance) {
return this.instance;
}
const builder = this.builder(options);
build && build(builder);
this._instance = builder.build();
return this._instance as BacktraceClient;
}
/**
* Returns created BacktraceClient instance if the instance exists.
* Otherwise undefined.
*/
public static get instance(): BacktraceClient | undefined {
return this._instance as BacktraceClient;
}
private captureUnhandledErrors(captureUnhandledExceptions = true, captureUnhandledRejections = true) {
if (captureUnhandledExceptions) {
this._exceptionHandler.captureManagedErrors(this);
}
if (captureUnhandledRejections) {
this._exceptionHandler.captureUnhandledPromiseRejections(this);
}
}
private initializeNativeCrashReporter(): CrashReporter | undefined {
if (!this.options.database?.enable) {
return;
}
if (!this.options.database?.captureNativeCrashes) {
return;
}
const fileSystem = this.fileSystem;
if (!fileSystem) {
return;
}
const submissionUrl = SubmissionUrlInformation.toJsonReportSubmissionUrl(this.options.url);
const crashReporter = new CrashReporter(fileSystem);
crashReporter.initialize(
Platform.select({
ios: SubmissionUrlInformation.toPlCrashReporterSubmissionUrl(submissionUrl),
android: SubmissionUrlInformation.toMinidumpSubmissionUrl(submissionUrl),
default: submissionUrl,
}),
this.options.database.path,
this.attributeManager.get('scoped').attributes,
this.attachments,
);
return crashReporter;
}
}