Skip to content

Commit a28268a

Browse files
committed
[v7] Inline injectReportDialog into showReportDialog and unify them
1 parent fe17ad1 commit a28268a

File tree

2 files changed

+36
-42
lines changed

2 files changed

+36
-42
lines changed

packages/browser/src/helpers.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { Dsn, getReportDialogEndpoint, ReportDialogOptions } from '@sentry/transport-base';
2-
import { logger } from '@sentry/utils';
3-
41
let ignoreOnError: number = 0;
52

63
/**
@@ -20,27 +17,3 @@ export function ignoreNextOnError(): void {
2017
ignoreOnError -= 1;
2118
});
2219
}
23-
24-
// TODO: Move it from helpers and remove helpers completely. Unfortunatelly Electron (or RN?) is importing it :(
25-
export function injectReportDialog(options: ReportDialogOptions & { onLoad?(): void } = {}): void {
26-
if (!options.eventId) {
27-
logger.error(`ReportDialog is missing EventID`);
28-
return;
29-
}
30-
31-
if (!options.dsn) {
32-
logger.error(`ReportDialog is missing DSN`);
33-
return;
34-
}
35-
36-
const script = document.createElement('script');
37-
script.async = true;
38-
script.src = getReportDialogEndpoint(new Dsn(options.dsn));
39-
40-
if (options.onLoad) {
41-
// eslint-disable-next-line @typescript-eslint/unbound-method
42-
script.onload = options.onLoad;
43-
}
44-
45-
(document.head || document.body).appendChild(script);
46-
}

packages/browser/src/sdk.ts

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ClientLike } from '@sentry/types';
22
import { captureException, getCarrier, getCurrentClient } from '@sentry/minimal';
33
import { addInstrumentationHandler, getGlobalObject, logger } from '@sentry/utils';
4-
import { ReportDialogOptions } from '@sentry/transport-base';
4+
import { Dsn, getReportDialogEndpoint, ReportDialogOptions } from '@sentry/transport-base';
55
import { InboundFilters } from '@sentry/integration-common-inboundfilters';
66
import { UserAgent } from '@sentry/integration-browser-useragent';
77
import { EventTargetWrap, TimersWrap, XHRWrap } from '@sentry/integration-browser-wrap';
@@ -16,7 +16,6 @@ import { LinkedErrors } from '@sentry/integration-browser-linkederrors';
1616
import { OnError, OnUnhandledRejection } from '@sentry/integration-browser-globalhandlers';
1717

1818
import { BrowserClient, BrowserOptions } from './client';
19-
import { injectReportDialog } from './helpers';
2019

2120
export const defaultIntegrations = [
2221
new EventTargetWrap(),
@@ -121,28 +120,50 @@ export function init(options: BrowserOptions = {}): void {
121120
*
122121
* @param options Everything is optional, we try to fetch all info need from the global scope.
123122
*/
124-
export function showReportDialog(options: ReportDialogOptions = {}, client?: ClientLike): void {
123+
export function showReportDialog(
124+
options: ReportDialogOptions & { onLoad?(): void } = {},
125+
customClient?: ClientLike,
126+
): void {
127+
const errPrefix = `Trying to call showReportDialog with`;
128+
125129
// doesn't work without a document (React Native)
126-
const document = getGlobalObject<Window>().document;
127-
if (!document) {
130+
const global = getGlobalObject<Window>();
131+
if (!global.document) {
132+
return;
133+
}
134+
135+
const client = customClient ?? getCurrentClient();
136+
if (!client) {
137+
return;
138+
}
139+
140+
options.eventId = options.eventId ?? client.lastEventId();
141+
options.dsn = options.dsn ?? client.getDsn()?.toString();
142+
143+
if (client.options.enabled === false) {
144+
logger.error(`${errPrefix} disabled client`);
145+
return;
146+
}
147+
148+
if (!options.eventId) {
149+
logger.error(`${errPrefix} missing EventID`);
128150
return;
129151
}
130152

131-
const usableClient = client ?? getCurrentClient();
132-
if (!usableClient) {
153+
if (!options.dsn) {
154+
logger.error(`${errPrefix} missing DSN`);
133155
return;
134156
}
135157

136-
options.eventId = options.eventId ?? usableClient.lastEventId();
137-
options.dsn = options.dsn ?? usableClient.getDsn()?.toString();
158+
const script = document.createElement('script');
159+
script.async = true;
160+
script.src = getReportDialogEndpoint(new Dsn(options.dsn));
138161

139-
// TODO: Should we keep `isEnabled` around?
140-
// if (!this._isEnabled()) {
141-
// logger.error('Trying to call showReportDialog with Sentry Client disabled');
142-
// return;
143-
// }
162+
if (options.onLoad) {
163+
script.onload = options.onLoad; // eslint-disable-line @typescript-eslint/unbound-method
164+
}
144165

145-
injectReportDialog(options);
166+
(global.document.head || global.document.body).appendChild(script);
146167
}
147168

148169
/**

0 commit comments

Comments
 (0)