Skip to content

Commit

Permalink
[v7] Inline injectReportDialog into showReportDialog and unify them
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Mar 23, 2021
1 parent fe17ad1 commit a28268a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 42 deletions.
27 changes: 0 additions & 27 deletions packages/browser/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { Dsn, getReportDialogEndpoint, ReportDialogOptions } from '@sentry/transport-base';
import { logger } from '@sentry/utils';

let ignoreOnError: number = 0;

/**
Expand All @@ -20,27 +17,3 @@ export function ignoreNextOnError(): void {
ignoreOnError -= 1;
});
}

// TODO: Move it from helpers and remove helpers completely. Unfortunatelly Electron (or RN?) is importing it :(
export function injectReportDialog(options: ReportDialogOptions & { onLoad?(): void } = {}): void {
if (!options.eventId) {
logger.error(`ReportDialog is missing EventID`);
return;
}

if (!options.dsn) {
logger.error(`ReportDialog is missing DSN`);
return;
}

const script = document.createElement('script');
script.async = true;
script.src = getReportDialogEndpoint(new Dsn(options.dsn));

if (options.onLoad) {
// eslint-disable-next-line @typescript-eslint/unbound-method
script.onload = options.onLoad;
}

(document.head || document.body).appendChild(script);
}
51 changes: 36 additions & 15 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ClientLike } from '@sentry/types';
import { captureException, getCarrier, getCurrentClient } from '@sentry/minimal';
import { addInstrumentationHandler, getGlobalObject, logger } from '@sentry/utils';
import { ReportDialogOptions } from '@sentry/transport-base';
import { Dsn, getReportDialogEndpoint, ReportDialogOptions } from '@sentry/transport-base';
import { InboundFilters } from '@sentry/integration-common-inboundfilters';
import { UserAgent } from '@sentry/integration-browser-useragent';
import { EventTargetWrap, TimersWrap, XHRWrap } from '@sentry/integration-browser-wrap';
Expand All @@ -16,7 +16,6 @@ import { LinkedErrors } from '@sentry/integration-browser-linkederrors';
import { OnError, OnUnhandledRejection } from '@sentry/integration-browser-globalhandlers';

import { BrowserClient, BrowserOptions } from './client';
import { injectReportDialog } from './helpers';

export const defaultIntegrations = [
new EventTargetWrap(),
Expand Down Expand Up @@ -121,28 +120,50 @@ export function init(options: BrowserOptions = {}): void {
*
* @param options Everything is optional, we try to fetch all info need from the global scope.
*/
export function showReportDialog(options: ReportDialogOptions = {}, client?: ClientLike): void {
export function showReportDialog(
options: ReportDialogOptions & { onLoad?(): void } = {},
customClient?: ClientLike,
): void {
const errPrefix = `Trying to call showReportDialog with`;

// doesn't work without a document (React Native)
const document = getGlobalObject<Window>().document;
if (!document) {
const global = getGlobalObject<Window>();
if (!global.document) {
return;
}

const client = customClient ?? getCurrentClient();
if (!client) {
return;
}

options.eventId = options.eventId ?? client.lastEventId();
options.dsn = options.dsn ?? client.getDsn()?.toString();

if (client.options.enabled === false) {
logger.error(`${errPrefix} disabled client`);
return;
}

if (!options.eventId) {
logger.error(`${errPrefix} missing EventID`);
return;
}

const usableClient = client ?? getCurrentClient();
if (!usableClient) {
if (!options.dsn) {
logger.error(`${errPrefix} missing DSN`);
return;
}

options.eventId = options.eventId ?? usableClient.lastEventId();
options.dsn = options.dsn ?? usableClient.getDsn()?.toString();
const script = document.createElement('script');
script.async = true;
script.src = getReportDialogEndpoint(new Dsn(options.dsn));

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

injectReportDialog(options);
(global.document.head || global.document.body).appendChild(script);
}

/**
Expand Down

0 comments on commit a28268a

Please sign in to comment.