-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathbackend.ts
152 lines (130 loc) · 4.37 KB
/
backend.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
import { BrowserOptions, Transports } from "@sentry/browser";
import { BrowserBackend } from "@sentry/browser/dist/backend";
import { BaseBackend, NoopTransport } from "@sentry/core";
import { Event, EventHint, Severity, Transport } from "@sentry/types";
import { Alert, YellowBox } from "react-native";
import { NativeTransport } from "./transports/native";
import { NATIVE } from "./wrapper";
/**
* Configuration options for the Sentry ReactNative SDK.
* @see ReactNativeFrontend for more information.
*/
export interface ReactNativeOptions extends BrowserOptions {
/**
* Enables native transport + device info + offline caching.
* Be careful, disabling this also breaks automatic release setting.
* This means you have to manage setting the release yourself.
* Defaults to `true`.
*/
enableNative?: boolean;
/**
* Enables native crashHandling. This only works if `enableNative` is `true`.
* Defaults to `true`.
*/
enableNativeCrashHandling?: boolean;
/** Maximum time to wait to drain the request queue, before the process is allowed to exit. */
shutdownTimeout?: number;
/** Should the native nagger alert be shown or not. */
enableNativeNagger?: boolean;
/** Should sessions be tracked to Sentry Health or not. */
enableAutoSessionTracking?: boolean;
/** The interval to end a session if the App goes to the background. */
sessionTrackingIntervalMillis?: number;
}
/** The Sentry ReactNative SDK Backend. */
export class ReactNativeBackend extends BaseBackend<BrowserOptions> {
private readonly _browserBackend: BrowserBackend;
/** Creates a new ReactNative backend instance. */
public constructor(protected readonly _options: ReactNativeOptions) {
super(_options);
this._browserBackend = new BrowserBackend(_options);
// This is a workaround for now using fetch on RN, this is a known issue in react-native and only generates a warning
YellowBox.ignoreWarnings(["Require cycle:"]);
if (this._options.enableNative) {
// tslint:disable-next-line: no-floating-promises
this._startWithOptions();
} else {
this._showCannotConnectDialog();
}
}
/**
* Starts native client with dsn and options
*/
private async _startWithOptions(): Promise<void> {
try {
await NATIVE.startWithOptions(this._options);
NATIVE.setLogLevel(this._options.debug ? 2 : 1);
} catch (_) {
this._showCannotConnectDialog();
}
}
/**
* If the user is in development mode, and the native nagger is enabled then it will show an alert.
*/
private _showCannotConnectDialog(): void {
if (__DEV__ && this._options.enableNativeNagger) {
Alert.alert(
"Sentry",
"Warning, could not connect to Sentry native SDK.\nIf you do not want to use the native component please pass `enableNative: false` in the options.\nVisit: https://docs.sentry.io/platforms/react-native/#linking for more details."
);
}
}
/**
* @inheritDoc
*/
protected _setupTransport(): Transport {
if (!this._options.dsn) {
// We return the noop transport here in case there is no Dsn.
return new NoopTransport();
}
const transportOptions = {
...this._options.transportOptions,
dsn: this._options.dsn
};
if (this._options.transport) {
return new this._options.transport(transportOptions);
}
if (this._isNativeTransportAvailable()) {
return new NativeTransport();
}
return new Transports.FetchTransport(transportOptions);
}
/**
* If true, native client is availabe and active
*/
private _isNativeTransportAvailable(): boolean {
return (
this._options.enableNative === true &&
NATIVE.isNativeClientAvailable() &&
NATIVE.isNativeTransportAvailable()
);
}
/**
* If native client is available it will trigger a native crash.
* Use this only for testing purposes.
*/
public nativeCrash(): void {
if (this._options.enableNative) {
NATIVE.crash();
}
}
/**
* @inheritDoc
*/
public eventFromException(
exception: any,
hint?: EventHint
): PromiseLike<Event> {
return this._browserBackend.eventFromException(exception, hint);
}
/**
* @inheritDoc
*/
public eventFromMessage(
message: string,
level: Severity = Severity.Info,
hint?: EventHint
): PromiseLike<Event> {
return this._browserBackend.eventFromMessage(message, level, hint);
}
}