-
-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathdevicecontext.ts
97 lines (82 loc) · 2.8 KB
/
devicecontext.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
/* eslint-disable complexity */
import type { Event, EventProcessor, Hub, Integration } from '@sentry/types';
import { logger, severityLevelFromString } from '@sentry/utils';
import { AppState } from 'react-native';
import { breadcrumbFromObject } from '../breadcrumb';
import type { NativeDeviceContextsResponse } from '../NativeRNSentry';
import { NATIVE } from '../wrapper';
/** Load device context from native. */
export class DeviceContext implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'DeviceContext';
/**
* @inheritDoc
*/
public name: string = DeviceContext.id;
/**
* @inheritDoc
*/
public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
addGlobalEventProcessor(async (event: Event) => {
const self = getCurrentHub().getIntegration(DeviceContext);
if (!self) {
return event;
}
let native: NativeDeviceContextsResponse | null = null;
try {
native = await NATIVE.fetchNativeDeviceContexts();
} catch (e) {
logger.log(`Failed to get device context from native: ${e}`);
}
if (!native) {
return event;
}
const nativeUser = native.user;
if (!event.user && nativeUser) {
event.user = nativeUser;
}
let nativeContexts = native.contexts;
if (AppState.currentState !== 'unknown') {
nativeContexts = nativeContexts || {};
nativeContexts.app = {
...nativeContexts.app,
in_foreground: AppState.currentState === 'active',
};
}
if (nativeContexts) {
event.contexts = { ...nativeContexts, ...event.contexts };
}
const nativeTags = native.tags;
if (nativeTags) {
event.tags = { ...nativeTags, ...event.tags };
}
const nativeExtra = native.extra;
if (nativeExtra) {
event.extra = { ...nativeExtra, ...event.extra };
}
const nativeFingerprint = native.fingerprint;
if (nativeFingerprint) {
event.fingerprint = (event.fingerprint ?? []).concat(
nativeFingerprint.filter(item => (event.fingerprint ?? []).indexOf(item) < 0),
);
}
const nativeLevel = typeof native['level'] === 'string' ? severityLevelFromString(native['level']) : undefined;
if (!event.level && nativeLevel) {
event.level = nativeLevel;
}
const nativeEnvironment = native['environment'];
if (!event.environment && nativeEnvironment) {
event.environment = nativeEnvironment;
}
const nativeBreadcrumbs = Array.isArray(native['breadcrumbs'])
? native['breadcrumbs'].map(breadcrumbFromObject)
: undefined;
if (nativeBreadcrumbs) {
event.breadcrumbs = nativeBreadcrumbs;
}
return event;
});
}
}