From 52466e0533ff3a4b313ddfa8e93440125ac2b970 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 12 Feb 2024 12:29:15 +0000 Subject: [PATCH 1/2] . --- packages/angular/src/errorhandler.ts | 7 +- packages/browser/src/exports.ts | 3 +- packages/browser/src/helpers.ts | 34 +-------- packages/browser/src/sdk.ts | 74 ++++++++++--------- packages/browser/test/unit/index.test.ts | 10 +-- .../test/integration/pages/reportDialog.tsx | 5 +- packages/react/src/errorboundary.tsx | 1 - 7 files changed, 54 insertions(+), 80 deletions(-) diff --git a/packages/angular/src/errorhandler.ts b/packages/angular/src/errorhandler.ts index b22536307e9a..e8c4350d971e 100644 --- a/packages/angular/src/errorhandler.ts +++ b/packages/angular/src/errorhandler.ts @@ -2,6 +2,7 @@ import { HttpErrorResponse } from '@angular/common/http'; import type { ErrorHandler as AngularErrorHandler } from '@angular/core'; import { Inject, Injectable } from '@angular/core'; import * as Sentry from '@sentry/browser'; +import type { ReportDialogOptions } from '@sentry/browser'; import type { Event } from '@sentry/types'; import { isString } from '@sentry/utils'; @@ -13,8 +14,7 @@ import { runOutsideAngular } from './zone'; export interface ErrorHandlerOptions { logErrors?: boolean; showDialog?: boolean; - // eslint-disable-next-line deprecation/deprecation - dialogOptions?: Omit; + dialogOptions?: Omit; /** * Custom implementation of error extraction from the raw value captured by the Angular. * @param error Value captured by Angular's ErrorHandler provider @@ -120,8 +120,7 @@ class SentryErrorHandler implements AngularErrorHandler { if (client && !this._registeredAfterSendEventHandler) { client.on('afterSendEvent', (event: Event) => { - if (!event.type) { - // eslint-disable-next-line deprecation/deprecation + if (!event.type && event.event_id) { Sentry.showReportDialog({ ...this._options.dialogOptions, eventId: event.event_id }); } }); diff --git a/packages/browser/src/exports.ts b/packages/browser/src/exports.ts index eda89ac56b1c..9356fa7c77a4 100644 --- a/packages/browser/src/exports.ts +++ b/packages/browser/src/exports.ts @@ -17,8 +17,7 @@ export type { export type { BrowserOptions } from './client'; -// eslint-disable-next-line deprecation/deprecation -export type { ReportDialogOptions } from './helpers'; +export type { ReportDialogOptions } from './sdk'; export { // eslint-disable-next-line deprecation/deprecation diff --git a/packages/browser/src/helpers.ts b/packages/browser/src/helpers.ts index 35930167672d..6b9df98f1751 100644 --- a/packages/browser/src/helpers.ts +++ b/packages/browser/src/helpers.ts @@ -1,7 +1,7 @@ import type { browserTracingIntegration } from '@sentry-internal/tracing'; import { BrowserTracing } from '@sentry-internal/tracing'; import { captureException, withScope } from '@sentry/core'; -import type { DsnLike, Integration, Mechanism, WrappedFunction } from '@sentry/types'; +import type { Integration, Mechanism, WrappedFunction } from '@sentry/types'; import { GLOBAL_OBJ, addExceptionMechanism, @@ -156,38 +156,6 @@ export function wrap( return sentryWrapped; } -/** - * All properties the report dialog supports - * - * @deprecated This type will be removed in the next major version of the Sentry SDK. `showReportDialog` will still be around, however the `eventId` option will now be required. - */ -export interface ReportDialogOptions { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - [key: string]: any; - eventId?: string; - dsn?: DsnLike; - user?: { - email?: string; - name?: string; - }; - lang?: string; - title?: string; - subtitle?: string; - subtitle2?: string; - labelName?: string; - labelEmail?: string; - labelComments?: string; - labelClose?: string; - labelSubmit?: string; - errorGeneric?: string; - errorFormEntry?: string; - successMessage?: string; - /** Callback after reportDialog showed up */ - onLoad?(this: void): void; - /** Callback after reportDialog closed */ - onClose?(this: void): void; -} - /** * This is a slim shim of `browserTracingIntegration` for the CDN bundles. * Since the actual functional integration uses a different code from `BrowserTracing`, diff --git a/packages/browser/src/sdk.ts b/packages/browser/src/sdk.ts index 39fabc92c8ec..871258fd49b7 100644 --- a/packages/browser/src/sdk.ts +++ b/packages/browser/src/sdk.ts @@ -1,15 +1,14 @@ -import type { Hub } from '@sentry/core'; +import { getCurrentScope } from '@sentry/core'; import { functionToStringIntegration, inboundFiltersIntegration } from '@sentry/core'; import { captureSession, getClient, - getCurrentHub, getIntegrationsToSetup, getReportDialogEndpoint, initAndBind, startSession, } from '@sentry/core'; -import type { Integration, Options, UserFeedback } from '@sentry/types'; +import type { DsnLike, Integration, Options, UserFeedback } from '@sentry/types'; import { addHistoryInstrumentationHandler, logger, @@ -20,7 +19,6 @@ import { import type { BrowserClientOptions, BrowserOptions } from './client'; import { BrowserClient } from './client'; import { DEBUG_BUILD } from './debug-build'; -import type { ReportDialogOptions } from './helpers'; import { WINDOW, wrap as internalWrap } from './helpers'; import { breadcrumbsIntegration } from './integrations/breadcrumbs'; import { dedupeIntegration } from './integrations/dedupe'; @@ -139,42 +137,52 @@ export function init(options: BrowserOptions = {}): void { } } -type NewReportDialogOptions = ReportDialogOptions & { eventId: string }; // eslint-disable-line - -interface ShowReportDialogFunction { - /** - * Present the user with a report dialog. - * - * @param options Everything is optional, we try to fetch all info need from the global scope. - */ - (options: NewReportDialogOptions): void; - - /** - * Present the user with a report dialog. - * - * @param options Everything is optional, we try to fetch all info need from the global scope. - * - * @deprecated Please always pass an `options` argument with `eventId`. The `hub` argument will not be used in the next version of the SDK. - */ - // eslint-disable-next-line deprecation/deprecation - (options?: ReportDialogOptions, hub?: Hub): void; +/** + * All properties the report dialog supports + */ +export interface ReportDialogOptions { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + [key: string]: any; + eventId: string; + dsn?: DsnLike; + user?: { + email?: string; + name?: string; + }; + lang?: string; + title?: string; + subtitle?: string; + subtitle2?: string; + labelName?: string; + labelEmail?: string; + labelComments?: string; + labelClose?: string; + labelSubmit?: string; + errorGeneric?: string; + errorFormEntry?: string; + successMessage?: string; + /** Callback after reportDialog showed up */ + onLoad?(this: void): void; + /** Callback after reportDialog closed */ + onClose?(this: void): void; } -export const showReportDialog: ShowReportDialogFunction = ( - // eslint-disable-next-line deprecation/deprecation - options: ReportDialogOptions = {}, - // eslint-disable-next-line deprecation/deprecation - hub: Hub = getCurrentHub(), -) => { +/** + * Present the user with a report dialog. + * + * @param options Everything is optional, we try to fetch all info need from the global scope. + */ +export function showReportDialog(options: ReportDialogOptions): void { // doesn't work without a document (React Native) if (!WINDOW.document) { DEBUG_BUILD && logger.error('Global document not defined in showReportDialog call'); return; } - // eslint-disable-next-line deprecation/deprecation - const { client, scope } = hub.getStackTop(); - const dsn = options.dsn || (client && client.getDsn()); + const scope = getCurrentScope(); + const client = scope.getClient(); + const dsn = client && client.getDsn(); + if (!dsn) { DEBUG_BUILD && logger.error('DSN not configured for showReportDialog call'); return; @@ -216,7 +224,7 @@ export const showReportDialog: ShowReportDialogFunction = ( } else { DEBUG_BUILD && logger.error('Not injecting report dialog. No injection point found in HTML'); } -}; +} /** * This function is here to be API compatible with the loader. diff --git a/packages/browser/test/unit/index.test.ts b/packages/browser/test/unit/index.test.ts index fe884ccc5438..b2c844976c1d 100644 --- a/packages/browser/test/unit/index.test.ts +++ b/packages/browser/test/unit/index.test.ts @@ -88,7 +88,7 @@ describe('SentryBrowser', () => { setCurrentClient(client); // eslint-disable-next-line deprecation/deprecation - showReportDialog(); + showReportDialog({ eventId: 'foobar' }); expect(getReportDialogEndpoint).toHaveBeenCalledTimes(1); expect(getReportDialogEndpoint).toHaveBeenCalledWith( @@ -103,7 +103,7 @@ describe('SentryBrowser', () => { const DIALOG_OPTION_USER = { email: 'option@example.com' }; // eslint-disable-next-line deprecation/deprecation - showReportDialog({ user: DIALOG_OPTION_USER }); + showReportDialog({ eventId: 'foobar', user: DIALOG_OPTION_USER }); expect(getReportDialogEndpoint).toHaveBeenCalledTimes(1); expect(getReportDialogEndpoint).toHaveBeenCalledWith( @@ -137,7 +137,7 @@ describe('SentryBrowser', () => { it('should call `onClose` when receiving `__sentry_reportdialog_closed__` MessageEvent', async () => { const onClose = jest.fn(); // eslint-disable-next-line deprecation/deprecation - showReportDialog({ onClose }); + showReportDialog({ eventId: 'foobar', onClose }); await waitForPostMessage('__sentry_reportdialog_closed__'); expect(onClose).toHaveBeenCalledTimes(1); @@ -152,7 +152,7 @@ describe('SentryBrowser', () => { throw new Error(); }); // eslint-disable-next-line deprecation/deprecation - showReportDialog({ onClose }); + showReportDialog({ eventId: 'foobar', onClose }); await waitForPostMessage('__sentry_reportdialog_closed__'); expect(onClose).toHaveBeenCalledTimes(1); @@ -165,7 +165,7 @@ describe('SentryBrowser', () => { it('should not call `onClose` for other MessageEvents', async () => { const onClose = jest.fn(); // eslint-disable-next-line deprecation/deprecation - showReportDialog({ onClose }); + showReportDialog({ eventId: 'foobar', onClose }); await waitForPostMessage('some_message'); expect(onClose).not.toHaveBeenCalled(); diff --git a/packages/nextjs/test/integration/pages/reportDialog.tsx b/packages/nextjs/test/integration/pages/reportDialog.tsx index b3337c0ee389..bfc9704c3aa9 100644 --- a/packages/nextjs/test/integration/pages/reportDialog.tsx +++ b/packages/nextjs/test/integration/pages/reportDialog.tsx @@ -1,9 +1,10 @@ -import { showReportDialog } from '@sentry/nextjs'; +import { captureException, showReportDialog } from '@sentry/nextjs'; const ReportDialogPage = (): JSX.Element => (