-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLazySentry.ts
71 lines (64 loc) · 2.68 KB
/
LazySentry.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
// https://d.sb/lazysentry
import type {BrowserOptions} from '@sentry/browser';
import {onSentryLoaded as onSentryLoadedForErrorBoundary} from './LazySentryErrorBoundary';
type SentryImportType = typeof import('./LazySentryImports');
let queue: Array<(sentry: SentryImportType) => void> = [];
let errorQueue: Array<Parameters<OnErrorEventHandlerNonNull>> = [];
let rejectionQueue: Array<PromiseRejectionEvent> = [];
// Before Sentry has loaded, these functions will push calls into a queue
// After Sentry has loaded, these will be replaced with the real functions
export let addBreadcrumb: SentryImportType['addBreadcrumb'] = (...args) => {
queue.push(x => x.addBreadcrumb(...args));
};
export let captureMessage: SentryImportType['captureMessage'] = (...args) => {
queue.push(x => x.captureMessage(...args));
return '';
};
export let captureException: SentryImportType['captureException'] = (
...args
) => {
queue.push(x => x.captureException(...args));
return '';
};
export let captureEvent: SentryImportType['captureEvent'] = (...args) => {
queue.push(x => x.captureEvent(...args));
return '';
};
export let configureScope: SentryImportType['configureScope'] = (...args) =>
queue.push(x => x.configureScope(...args));
export let showReportDialog: SentryImportType['showReportDialog'] = (...args) =>
queue.push(x => x.showReportDialog(...args));
export let withScope: SentryImportType['withScope'] = (...args) =>
queue.push(x => x.withScope(...args));
export function init(options: BrowserOptions) {
const oldOnError = window.onerror;
const oldOnUnhandledRejection = window.onunhandledrejection;
window.onerror = (...args) => errorQueue.push(args);
window.onunhandledrejection = (e: PromiseRejectionEvent) =>
rejectionQueue.push(e);
import(
/* webpackChunkName: "sentry" */
/* webpackPreload: true */
'./LazySentryImports'
).then(Sentry => {
window.onerror = oldOnError;
window.onunhandledrejection = oldOnUnhandledRejection;
Sentry.init({
...options,
integrations: [new Sentry.BrowserTracing()],
});
// Override the placeholder functions with the real ones
addBreadcrumb = Sentry.addBreadcrumb;
captureMessage = Sentry.captureMessage;
captureException = Sentry.captureException;
captureEvent = Sentry.captureEvent;
configureScope = Sentry.configureScope;
showReportDialog = Sentry.showReportDialog;
withScope = Sentry.withScope;
onSentryLoadedForErrorBoundary(Sentry.ErrorBoundary);
// Replay queued calls and errors through Sentry's handlers
queue.forEach(call => call(Sentry));
errorQueue.forEach(x => window.onerror?.(...x));
rejectionQueue.forEach(e => window.onunhandledrejection?.(e));
});
}