-
-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathdebugsymbolicatorutils.ts
More file actions
104 lines (93 loc) · 2.94 KB
/
Copy pathdebugsymbolicatorutils.ts
File metadata and controls
104 lines (93 loc) · 2.94 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
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
import type { StackFrame as SentryStackFrame } from '@sentry/core';
import { logger } from '@sentry/core';
import { ReactNativeLibraries } from '../utils/rnlibraries';
import { createStealthXhr, XHR_READYSTATE_DONE } from '../utils/xhr';
import type * as ReactNative from '../vendor/react-native';
/**
* Fetches source context for the Sentry Middleware (/__sentry/context)
*
* @param frame StackFrame
* @param getDevServer function from RN to get DevServer URL
*/
export async function fetchSourceContext(frames: SentryStackFrame[]): Promise<SentryStackFrame[]> {
return new Promise(resolve => {
try {
const xhr = createStealthXhr();
if (!xhr) {
resolve(frames);
return;
}
const url = getSentryMetroSourceContextUrl();
if (!url) {
logger.error('Could not fetch source context. No dev server URL found.');
resolve(frames);
return;
}
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({ stack: frames }));
xhr.onreadystatechange = (): void => {
if (xhr.readyState === XHR_READYSTATE_DONE) {
if (xhr.status !== 200) {
resolve(frames);
}
try {
const response: { stack?: SentryStackFrame[] } = JSON.parse(xhr.responseText);
if (Array.isArray(response.stack)) {
resolve(response.stack);
} else {
resolve(frames);
}
} catch (error) {
resolve(frames);
}
}
};
xhr.onerror = (): void => {
resolve(frames);
};
} catch (error) {
logger.error('Could not fetch source context.', error);
resolve(frames);
}
});
}
function getSentryMetroSourceContextUrl(): string | undefined {
const devServer = getDevServer();
if (!devServer) {
return undefined;
}
return `${devServer.url}__sentry/context`;
}
/**
* Loads and calls RN Core Devtools parseErrorStack function.
*/
export function parseErrorStack(errorStack: string): Array<ReactNative.StackFrame> {
if (!ReactNativeLibraries.Devtools) {
throw new Error('React Native Devtools not available.');
}
return ReactNativeLibraries.Devtools.parseErrorStack(errorStack);
}
/**
* Loads and calls RN Core Devtools symbolicateStackTrace function.
*/
export function symbolicateStackTrace(
stack: Array<ReactNative.StackFrame>,
extraData?: Record<string, unknown>,
): Promise<ReactNative.SymbolicatedStackTrace> {
if (!ReactNativeLibraries.Devtools) {
throw new Error('React Native Devtools not available.');
}
return ReactNativeLibraries.Devtools.symbolicateStackTrace(stack, extraData);
}
/**
* Loads and returns the RN DevServer URL.
*/
export function getDevServer(): ReactNative.DevServerInfo | undefined {
try {
return ReactNativeLibraries.Devtools?.getDevServer();
} catch (_oO) {
// We can't load devserver URL
}
return undefined;
}