-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathsentry.ts
More file actions
75 lines (66 loc) · 1.92 KB
/
sentry.ts
File metadata and controls
75 lines (66 loc) · 1.92 KB
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
import * as Sentry from '@sentry/react-native';
import DeviceInfo from 'react-native-device-info';
import appPackage from './package.json';
import {hash, anonymizeYTApiEntityId} from 'util/util';
import type {ErrorEvent, EventHint} from '@sentry/core';
export const hasSentryDsn = () => appPackage.config.SENTRY_DSN.length > 0;
const getDeviceId = async () => DeviceInfo.getUniqueId();
export const initSentry = async (dsn: string) => {
Sentry.init({
dsn,
sendDefaultPii: false,
integrations: [Sentry.breadcrumbsIntegration({console: false})],
tracesSampleRate: 0.1,
profilesSampleRate: 0.1,
// @ts-ignore
beforeSend(event, hint) {
if (isInvalidTokenError(hint) || !isAppCodeError(event)) {
return null;
}
return sanitizeEventUrls(event);
},
});
const deviceId = await getDeviceId();
Sentry.setUser({
id: hash(deviceId, 12),
tablet: DeviceInfo.isTablet(),
geo: {
country_code: '',
region: '',
city: '',
},
});
};
export const captureException = (error: Error) => {
if (hasSentryDsn()) {
Sentry.captureException(error);
}
};
function isAppCodeError(event: ErrorEvent) {
const frames = event.exception?.values?.[0]?.stacktrace?.frames;
if (Array.isArray(frames)) {
return frames.some(frame => !frame.filename?.includes('node_modules'));
}
return true;
}
function isInvalidTokenError(hint: EventHint) {
const error = hint?.originalException;
if (!error || typeof error !== 'object') {
return false;
}
if ('status' in error && typeof error.status === 'number') {
return error.status === 401;
}
return false;
}
function sanitizeEventUrls(event: ErrorEvent) {
const ev = {...event};
if (Array.isArray(ev.breadcrumbs)) {
ev.breadcrumbs.forEach(b => {
if((b.category === 'xhr' || b.category === 'fetch') && b.data?.url) {
b.data.url = anonymizeYTApiEntityId(b.data.url);
}
});
}
return ev;
}