Skip to content

Commit d5568d8

Browse files
authored
ref(feedback): Make eventId optional and use lastEventId in report dialog (#12029)
1 parent 6f81262 commit d5568d8

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

packages/angular/src/errorhandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { runOutsideAngular } from './zone';
1414
export interface ErrorHandlerOptions {
1515
logErrors?: boolean;
1616
showDialog?: boolean;
17-
dialogOptions?: Omit<ReportDialogOptions, 'eventId'>;
17+
dialogOptions?: ReportDialogOptions;
1818
/**
1919
* Custom implementation of error extraction from the raw value captured by the Angular.
2020
* @param error Value captured by Angular's ErrorHandler provider

packages/browser/src/sdk.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getIntegrationsToSetup,
77
getReportDialogEndpoint,
88
initAndBind,
9+
lastEventId,
910
startSession,
1011
} from '@sentry/core';
1112
import type { DsnLike, Integration, Options, UserFeedback } from '@sentry/types';
@@ -168,7 +169,7 @@ export function init(browserOptions: BrowserOptions = {}): void {
168169
export interface ReportDialogOptions {
169170
// eslint-disable-next-line @typescript-eslint/no-explicit-any
170171
[key: string]: any;
171-
eventId: string;
172+
eventId?: string;
172173
dsn?: DsnLike;
173174
user?: {
174175
email?: string;
@@ -197,7 +198,7 @@ export interface ReportDialogOptions {
197198
*
198199
* @param options Everything is optional, we try to fetch all info need from the global scope.
199200
*/
200-
export function showReportDialog(options: ReportDialogOptions): void {
201+
export function showReportDialog(options: ReportDialogOptions = {}): void {
201202
// doesn't work without a document (React Native)
202203
if (!WINDOW.document) {
203204
DEBUG_BUILD && logger.error('Global document not defined in showReportDialog call');
@@ -220,6 +221,13 @@ export function showReportDialog(options: ReportDialogOptions): void {
220221
};
221222
}
222223

224+
if (!options.eventId) {
225+
const eventId = lastEventId();
226+
if (eventId) {
227+
options.eventId = eventId;
228+
}
229+
}
230+
223231
const script = WINDOW.document.createElement('script');
224232
script.async = true;
225233
script.crossOrigin = 'anonymous';

packages/browser/test/unit/index.test.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getIsolationScope,
55
getReportDialogEndpoint,
66
inboundFiltersIntegration,
7+
lastEventId,
78
} from '@sentry/core';
89
import * as utils from '@sentry/utils';
910

@@ -94,7 +95,7 @@ describe('SentryBrowser', () => {
9495
getCurrentScope().setUser(EX_USER);
9596
setCurrentClient(client);
9697

97-
showReportDialog({ eventId: 'foobar' });
98+
showReportDialog();
9899

99100
expect(getReportDialogEndpoint).toHaveBeenCalledTimes(1);
100101
expect(getReportDialogEndpoint).toHaveBeenCalledWith(
@@ -103,12 +104,37 @@ describe('SentryBrowser', () => {
103104
);
104105
});
105106

107+
it('uses `lastEventId` from isolation scope', async () => {
108+
setCurrentClient(client);
109+
const eventId = captureException(new Error('Some error'));
110+
await flush(2000);
111+
112+
showReportDialog();
113+
114+
expect(eventId).toEqual(lastEventId());
115+
expect(getReportDialogEndpoint).toHaveBeenCalledTimes(1);
116+
expect(getReportDialogEndpoint).toHaveBeenCalledWith(expect.any(Object), expect.objectContaining({ eventId }));
117+
});
118+
119+
it('uses the passed in `eventId` over `lastEventId`', async () => {
120+
setCurrentClient(client);
121+
captureException(new Error('Some error'));
122+
await flush(2000);
123+
124+
showReportDialog({ eventId: 'foobar' });
125+
expect(getReportDialogEndpoint).toHaveBeenCalledTimes(1);
126+
expect(getReportDialogEndpoint).toHaveBeenCalledWith(
127+
expect.any(Object),
128+
expect.objectContaining({ eventId: 'foobar' }),
129+
);
130+
});
131+
106132
it('prioritizes options user over scope user', () => {
107133
getCurrentScope().setUser(EX_USER);
108134
setCurrentClient(client);
109135

110136
const DIALOG_OPTION_USER = { email: '[email protected]' };
111-
showReportDialog({ eventId: 'foobar', user: DIALOG_OPTION_USER });
137+
showReportDialog({ user: DIALOG_OPTION_USER });
112138

113139
expect(getReportDialogEndpoint).toHaveBeenCalledTimes(1);
114140
expect(getReportDialogEndpoint).toHaveBeenCalledWith(
@@ -142,7 +168,7 @@ describe('SentryBrowser', () => {
142168
it('should call `onClose` when receiving `__sentry_reportdialog_closed__` MessageEvent', async () => {
143169
const onClose = jest.fn();
144170

145-
showReportDialog({ eventId: 'foobar', onClose });
171+
showReportDialog({ onClose });
146172

147173
await waitForPostMessage('__sentry_reportdialog_closed__');
148174
expect(onClose).toHaveBeenCalledTimes(1);
@@ -157,7 +183,7 @@ describe('SentryBrowser', () => {
157183
throw new Error();
158184
});
159185

160-
showReportDialog({ eventId: 'foobar', onClose });
186+
showReportDialog({ onClose });
161187

162188
await waitForPostMessage('__sentry_reportdialog_closed__');
163189
expect(onClose).toHaveBeenCalledTimes(1);
@@ -170,7 +196,7 @@ describe('SentryBrowser', () => {
170196
it('should not call `onClose` for other MessageEvents', async () => {
171197
const onClose = jest.fn();
172198

173-
showReportDialog({ eventId: 'foobar', onClose });
199+
showReportDialog({ onClose });
174200

175201
await waitForPostMessage('some_message');
176202
expect(onClose).not.toHaveBeenCalled();

packages/react/src/errorboundary.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export type ErrorBoundaryProps = {
2929
* Options to be passed into the Sentry report dialog.
3030
* No-op if {@link showDialog} is false.
3131
*/
32-
dialogOptions?: Omit<ReportDialogOptions, 'eventId'> | undefined;
32+
dialogOptions?: ReportDialogOptions | undefined;
3333
/**
3434
* A fallback component that gets rendered when the error boundary encounters an error.
3535
*

0 commit comments

Comments
 (0)