Skip to content

Commit

Permalink
ref: Deprecate lastEventId() (#10043)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst authored Jan 4, 2024
1 parent b1458d6 commit 8fdf340
Show file tree
Hide file tree
Showing 22 changed files with 93 additions and 10 deletions.
19 changes: 19 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ Instead, import this directly from `@sentry/utils`.

Generally, in most cases you should probably use `continueTrace` instead, which abstracts this away from you and handles scope propagation for you.

## Deprecate `lastEventId()`

Instead, if you need the ID of a recently captured event, we recommend using `beforeSend` instead:

```ts
import * as Sentry from "@sentry/browser";

Sentry.init({
dsn: "__DSN__",
beforeSend(event, hint) {
const lastCapturedEventId = event.event_id;

// Do something with `lastCapturedEventId` here

return event;
},
});
```

## Deprecate `timestampWithMs` export - #7878

The `timestampWithMs` util is deprecated in favor of using `timestampInSeconds`.
Expand Down
4 changes: 3 additions & 1 deletion packages/angular/src/errorhandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { runOutsideAngular } from './zone';
export interface ErrorHandlerOptions {
logErrors?: boolean;
showDialog?: boolean;
dialogOptions?: Sentry.ReportDialogOptions;
// eslint-disable-next-line deprecation/deprecation
dialogOptions?: Omit<Sentry.ReportDialogOptions, 'eventId'>;
/**
* Custom implementation of error extraction from the raw value captured by the Angular.
* @param error Value captured by Angular's ErrorHandler provider
Expand Down Expand Up @@ -120,6 +121,7 @@ class SentryErrorHandler implements AngularErrorHandler {
if (client && client.on && !this._registeredAfterSendEventHandler) {
client.on('afterSendEvent', (event: Event) => {
if (!event.type) {
// eslint-disable-next-line deprecation/deprecation
Sentry.showReportDialog({ ...this._options.dialogOptions, eventId: event.event_id });
}
});
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export {
makeNodeTransport,
defaultIntegrations,
defaultStackParser,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
flush,
close,
Expand Down
4 changes: 4 additions & 0 deletions packages/astro/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export declare const defaultStackParser: StackParser;

export declare function close(timeout?: number | undefined): PromiseLike<boolean>;
export declare function flush(timeout?: number | undefined): PromiseLike<boolean>;

/**
* @deprecated This function will be removed in the next major version of the Sentry SDK.
*/
export declare function lastEventId(): string | undefined;

export default sentryAstro;
3 changes: 3 additions & 0 deletions packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export type {
} from '@sentry/types';

export type { BrowserOptions } from './client';

// eslint-disable-next-line deprecation/deprecation
export type { ReportDialogOptions } from './helpers';

export {
Expand All @@ -39,6 +41,7 @@ export {
getClient,
getCurrentScope,
Hub,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
makeMain,
Scope,
Expand Down
2 changes: 2 additions & 0 deletions packages/browser/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ export function wrap(

/**
* 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
Expand Down
37 changes: 30 additions & 7 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,32 @@ export function init(options: BrowserOptions = {}): void {
}
}

/**
* 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 = {}, hub: Hub = getCurrentHub()): 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;
}

export const showReportDialog: ShowReportDialogFunction = (
// eslint-disable-next-line deprecation/deprecation
options: ReportDialogOptions = {},
hub: Hub = getCurrentHub(),
) => {
// doesn't work without a document (React Native)
if (!WINDOW.document) {
DEBUG_BUILD && logger.error('Global document not defined in showReportDialog call');
Expand All @@ -159,7 +179,10 @@ export function showReportDialog(options: ReportDialogOptions = {}, hub: Hub = g
};
}

// TODO(v8): Remove this entire if statement. `eventId` will be a required option.
// eslint-disable-next-line deprecation/deprecation
if (!options.eventId) {
// eslint-disable-next-line deprecation/deprecation
options.eventId = hub.lastEventId();
}

Expand Down Expand Up @@ -192,7 +215,7 @@ export function showReportDialog(options: ReportDialogOptions = {}, hub: Hub = g
} 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.
Expand Down
5 changes: 5 additions & 0 deletions packages/browser/test/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ describe('SentryBrowser', () => {
getCurrentScope().setUser(EX_USER);
getCurrentHub().bindClient(client);

// eslint-disable-next-line deprecation/deprecation
showReportDialog();

expect(getReportDialogEndpoint).toHaveBeenCalledTimes(1);
Expand All @@ -102,6 +103,7 @@ describe('SentryBrowser', () => {
getCurrentHub().bindClient(client);

const DIALOG_OPTION_USER = { email: 'option@example.com' };
// eslint-disable-next-line deprecation/deprecation
showReportDialog({ user: DIALOG_OPTION_USER });

expect(getReportDialogEndpoint).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -135,6 +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 });

await waitForPostMessage('__sentry_reportdialog_closed__');
Expand All @@ -149,6 +152,7 @@ describe('SentryBrowser', () => {
const onClose = jest.fn(() => {
throw new Error();
});
// eslint-disable-next-line deprecation/deprecation
showReportDialog({ onClose });

await waitForPostMessage('__sentry_reportdialog_closed__');
Expand All @@ -161,6 +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 });

await waitForPostMessage('some_message');
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export {
getGlobalScope,
getIsolationScope,
Hub,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
makeMain,
runWithAsyncContext,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ export async function close(timeout?: number): Promise<boolean> {
* This is the getter for lastEventId.
*
* @returns The last event id of a captured event.
* @deprecated This function will be removed in the next major version of the Sentry SDK.
*/
export function lastEventId(): string | undefined {
return getCurrentHub().lastEventId();
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
// eslint-disable-next-line deprecation/deprecation
configureScope,
flush,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
startTransaction,
setContext,
Expand Down
1 change: 1 addition & 0 deletions packages/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export {
getGlobalScope,
getIsolationScope,
Hub,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
makeMain,
runWithAsyncContext,
Expand Down
1 change: 1 addition & 0 deletions packages/node-experimental/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export {
captureMessage,
addGlobalEventProcessor,
addEventProcessor,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
setContext,
setExtra,
Expand Down
5 changes: 4 additions & 1 deletion packages/node-experimental/src/sdk/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export function withIsolationScope<T>(callback: (isolationScope: Scope) => T): T
});
}

/** Get the ID of the last sent error event. */
/**
* Get the ID of the last sent error event.
* @deprecated This function will be removed in the next major version of the Sentry SDK.
*/
export function lastEventId(): string | undefined {
return getCurrentScope().lastEventId();
}
Expand Down
1 change: 1 addition & 0 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export {
getGlobalScope,
getIsolationScope,
Hub,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
makeMain,
runWithAsyncContext,
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/errorboundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export type ErrorBoundaryProps = {
* Options to be passed into the Sentry report dialog.
* No-op if {@link showDialog} is false.
*/
dialogOptions?: ReportDialogOptions | undefined;
// eslint-disable-next-line deprecation/deprecation
dialogOptions?: Omit<ReportDialogOptions, 'eventId'> | undefined;
/**
* A fallback component that gets rendered when the error boundary encounters an error.
*
Expand Down Expand Up @@ -111,6 +112,7 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
this._openFallbackReportDialog = false;
client.on('afterSendEvent', event => {
if (!event.type && event.event_id === this._lastEventId) {
// eslint-disable-next-line deprecation/deprecation
showReportDialog({ ...props.dialogOptions, eventId: this._lastEventId });
}
});
Expand Down
1 change: 1 addition & 0 deletions packages/remix/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export {
makeNodeTransport,
defaultIntegrations,
defaultStackParser,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
flush,
close,
Expand Down
5 changes: 5 additions & 0 deletions packages/remix/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ declare const runtime: 'client' | 'server';

export const close = runtime === 'client' ? clientSdk.close : serverSdk.close;
export const flush = runtime === 'client' ? clientSdk.flush : serverSdk.flush;

/**
* @deprecated This function will be removed in the next major version of the Sentry SDK.
*/
// eslint-disable-next-line deprecation/deprecation
export const lastEventId = runtime === 'client' ? clientSdk.lastEventId : serverSdk.lastEventId;
1 change: 1 addition & 0 deletions packages/serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export {
flush,
getSentryRelease,
init,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
DEFAULT_USER_INCLUDES,
addRequestDataToEvent,
Expand Down
4 changes: 4 additions & 0 deletions packages/sveltekit/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ export declare const defaultStackParser: StackParser;

export declare function close(timeout?: number | undefined): PromiseLike<boolean>;
export declare function flush(timeout?: number | undefined): PromiseLike<boolean>;

/**
* @deprecated This function will be removed in the next major version of the Sentry SDK.
*/
export declare function lastEventId(): string | undefined;
1 change: 1 addition & 0 deletions packages/sveltekit/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export {
makeNodeTransport,
defaultIntegrations,
defaultStackParser,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
flush,
close,
Expand Down
1 change: 1 addition & 0 deletions packages/vercel-edge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export {
getGlobalScope,
getIsolationScope,
Hub,
// eslint-disable-next-line deprecation/deprecation
lastEventId,
makeMain,
runWithAsyncContext,
Expand Down

0 comments on commit 8fdf340

Please sign in to comment.