1 | import { supportsReportingObserver, GLOBAL_OBJ } from '@sentry/utils';
|
2 |
|
3 | const WINDOW = GLOBAL_OBJ ;
|
4 |
|
5 |
|
6 | class ReportingObserver {
|
7 | |
8 |
|
9 |
|
10 | static __initStatic() {this.id = 'ReportingObserver';}
|
11 |
|
12 | |
13 |
|
14 |
|
15 | __init() {this.name = ReportingObserver.id;}
|
16 |
|
17 | |
18 |
|
19 |
|
20 |
|
21 | |
22 |
|
23 |
|
24 | constructor(
|
25 | _options
|
26 |
|
27 | = {
|
28 | types: ['crash', 'deprecation', 'intervention'],
|
29 | },
|
30 | ) {this._options = _options;ReportingObserver.prototype.__init.call(this);}
|
31 |
|
32 | |
33 |
|
34 |
|
35 | setupOnce(_, getCurrentHub) {
|
36 | if (!supportsReportingObserver()) {
|
37 | return;
|
38 | }
|
39 |
|
40 | this._getCurrentHub = getCurrentHub;
|
41 |
|
42 |
|
43 | const observer = new (WINDOW ).ReportingObserver(this.handler.bind(this), {
|
44 | buffered: true,
|
45 | types: this._options.types,
|
46 | });
|
47 |
|
48 |
|
49 | observer.observe();
|
50 | }
|
51 |
|
52 | |
53 |
|
54 |
|
55 | handler(reports) {
|
56 | const hub = this._getCurrentHub && this._getCurrentHub();
|
57 | if (!hub || !hub.getIntegration(ReportingObserver)) {
|
58 | return;
|
59 | }
|
60 | for (const report of reports) {
|
61 | hub.withScope(scope => {
|
62 | scope.setExtra('url', report.url);
|
63 |
|
64 | const label = `ReportingObserver [${report.type}]`;
|
65 | let details = 'No details available';
|
66 |
|
67 | if (report.body) {
|
68 |
|
69 | const plainBody
|
70 |
|
71 | = {};
|
72 |
|
73 |
|
74 | for (const prop in report.body) {
|
75 | plainBody[prop] = report.body[prop];
|
76 | }
|
77 |
|
78 | scope.setExtra('body', plainBody);
|
79 |
|
80 | if (report.type === 'crash') {
|
81 | const body = report.body ;
|
82 |
|
83 | details = [body.crashId || '', body.reason || ''].join(' ').trim() || details;
|
84 | } else {
|
85 | const body = report.body ;
|
86 | details = body.message || details;
|
87 | }
|
88 | }
|
89 |
|
90 | hub.captureMessage(`${label}: ${details}`);
|
91 | });
|
92 | }
|
93 | }
|
94 | } ReportingObserver.__initStatic();
|
95 |
|
96 | export { ReportingObserver };
|
97 |
|