forked from expo/sentry-expo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (141 loc) · 4.64 KB
/
index.js
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
/* @flow */
export * from '@sentry/react-native';
import Constants from 'expo-constants';
import { Platform } from 'react-native';
import * as Sentry from '@sentry/react-native';
import { RewriteFrames } from '@sentry/integrations';
/**
* Expo bundles are hosted on cloudfront. Expo bundle filename will change
* at some point in the future in order to be able to delete this code.
*/
function isPublishedExpoUrl(url) {
return url.includes('https://d1wp6m56sqw74a.cloudfront.net');
}
function normalizeUrl(url) {
if (isPublishedExpoUrl(url)) {
return `app:///main.${Platform.OS}.bundle`;
} else {
return url;
}
}
class ExpoIntegration {
static id = 'ExpoIntegration';
name = ExpoIntegration.id;
setupOnce() {
Sentry.setExtras({
manifest: Constants.manifest,
deviceYearClass: Constants.deviceYearClass,
linkingUri: Constants.linkingUri,
});
Sentry.setTags({
deviceId: Constants.deviceId,
appOwnership: Constants.appOwnership,
expoVersion: Constants.expoVersion,
});
if (Constants.manifest.releaseChannel) {
Sentry.setTag('expoReleaseChannel', Constants.manifest.releaseChannel);
}
if (Constants.manifest.version) {
Sentry.setTag('expoAppVersion', Constants.manifest.version);
}
if (Constants.manifest.publishedTime) {
Sentry.setTag('expoAppPublishedTime', Constants.manifest.publishedTime);
}
if (Constants.sdkVersion) {
Sentry.setTag('expoSdkVersion', Constants.sdkVersion);
}
const defaultHandler =
(ErrorUtils.getGlobalHandler && ErrorUtils.getGlobalHandler()) || ErrorUtils._globalHandler;
ErrorUtils.setGlobalHandler((error, isFatal) => {
// On Android, the Expo bundle filepath cannot be handled by TraceKit,
// so we normalize it to use the same filepath that we use on Expo iOS.
if (Platform.OS === 'android') {
error.stack = error.stack.replace(
/\/.*\/\d+\.\d+.\d+\/cached\-bundle\-experience\-/g,
'https://d1wp6m56sqw74a.cloudfront.net:443/'
);
}
Sentry.getCurrentHub().withScope(scope => {
if (isFatal) {
scope.setLevel(Sentry.Severity.Fatal);
}
Sentry.getCurrentHub().captureException(error, {
originalException: error,
});
});
const client = Sentry.getCurrentHub().getClient();
// If in dev, we call the default handler anyway and hope the error will be sent
// Just for a better dev experience
if (client && !__DEV__) {
client
.flush(client.getOptions().shutdownTimeout || 2000)
.then(() => {
defaultHandler(error, isFatal);
})
.catch(e => {
logger.error(e);
});
} else {
// If there is no client something is fishy, anyway we call the default handler
defaultHandler(error, isFatal);
}
});
Sentry.addGlobalEventProcessor(function(event, hint) {
var that = Sentry.getCurrentHub().getIntegration(ExpoIntegration);
if (that) {
let additionalDeviceInformation = {};
if (Platform.OS === 'ios') {
additionalDeviceInformation = {
model: Constants.platform.ios.model,
};
} else {
additionalDeviceInformation = {
model: 'n/a',
};
}
event.contexts = {
...(event.contexts || {}),
device: {
deviceName: Constants.deviceName,
simulator: !Constants.isDevice,
...additionalDeviceInformation,
},
os: {
name: Platform.OS === 'ios' ? 'iOS' : 'Android',
version: `${Platform.Version}`,
},
};
}
return event;
});
}
}
const originalSentryInit = Sentry.init;
export const init = (options = {}) => {
options.integrations = [
new Sentry.Integrations.ReactNativeErrorHandlers({
onerror: false,
onunhandledrejection: true,
}),
new ExpoIntegration(),
new RewriteFrames({
iteratee: frame => {
if (frame.filename) {
frame.filename = normalizeUrl(frame.filename);
}
return frame;
},
}),
];
const release = Constants.manifest.revisionId || 'UNVERSIONED';
// Bail out automatically if the app isn't deployed
if (release === 'UNVERSIONED' && !options.enableInExpoDevelopment) {
options.enabled = false;
console.log(
'[sentry-expo] Disabled Sentry in development. Note you can set Sentry.init({ enableInExpoDevelopment: true });'
);
}
// We don't want to have the native nagger.
options.enableNativeNagger = false;
return originalSentryInit({ ...options, release });
};