Skip to content

Commit ea5563b

Browse files
committed
Move logic inside integration
1 parent bfe819d commit ea5563b

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

packages/browser/src/client.ts

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { BaseClient, getCurrentHub, getEnvelopeEndpointWithUrlEncodedAuth, Scope, SDK_VERSION } from '@sentry/core';
1+
import { BaseClient, getEnvelopeEndpointWithUrlEncodedAuth, Scope, SDK_VERSION } from '@sentry/core';
22
import { ClientOptions, Event, EventHint, Options, Severity, SeverityLevel } from '@sentry/types';
3-
import { createClientReportEnvelope, dsnToString, getEventDescription, logger, serializeEnvelope } from '@sentry/utils';
3+
import { createClientReportEnvelope, dsnToString, logger, serializeEnvelope } from '@sentry/utils';
44

55
import { eventFromException, eventFromMessage } from './eventbuilder';
66
import { WINDOW } from './helpers';
@@ -101,27 +101,10 @@ export class BrowserClient extends BaseClient<BrowserClientOptions> {
101101
// bundles, if it is not used by the SDK.
102102
// This all sadly is a bit ugly, but we currently don't have a "pre-send" hook on the integrations so we do it this
103103
// way for now.
104-
const breadcrumbIntegration = this.getIntegrationById(BREADCRUMB_INTEGRATION_ID) as Breadcrumbs | null;
105-
if (
106-
breadcrumbIntegration &&
107-
// We check for definedness of `options`, even though it is not strictly necessary, because that access to
108-
// `.sentry` below does not throw, in case users provided their own integration with id "Breadcrumbs" that does
109-
// not have an`options` field
110-
breadcrumbIntegration.options &&
111-
breadcrumbIntegration.options.sentry
112-
) {
113-
getCurrentHub().addBreadcrumb(
114-
{
115-
category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`,
116-
event_id: event.event_id,
117-
level: event.level,
118-
message: getEventDescription(event),
119-
},
120-
{
121-
event,
122-
},
123-
);
124-
}
104+
const breadcrumbIntegration = this.getIntegrationById(BREADCRUMB_INTEGRATION_ID) as Breadcrumbs | undefined;
105+
// We check for definedness of `addSentryBreadcrumb` in case users provided their own integration with id
106+
// "Breadcrumbs" that does not have this function.
107+
breadcrumbIntegration?.addSentryBreadcrumb?.(event);
125108

126109
super.sendEvent(event, hint);
127110
}

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
22
/* eslint-disable max-lines */
33
import { getCurrentHub } from '@sentry/core';
4-
import { Integration } from '@sentry/types';
4+
import { Event, Integration } from '@sentry/types';
55
import {
66
addInstrumentationHandler,
7+
getEventDescription,
78
htmlTreeAsString,
89
parseUrl,
910
safeJoin,
@@ -85,6 +86,25 @@ export class Breadcrumbs implements Integration {
8586
addInstrumentationHandler('history', _historyBreadcrumb);
8687
}
8788
}
89+
90+
/**
91+
* Adds a breadcrumb for Sentry events or transactions if this option is enabled.
92+
*/
93+
public addSentryBreadcrumb(event: Event): void {
94+
if (this.options.sentry) {
95+
getCurrentHub().addBreadcrumb(
96+
{
97+
category: `sentry.${event.type === 'transaction' ? 'transaction' : 'event'}`,
98+
event_id: event.event_id,
99+
level: event.level,
100+
message: getEventDescription(event),
101+
},
102+
{
103+
event,
104+
},
105+
);
106+
}
107+
}
88108
}
89109

90110
/**

0 commit comments

Comments
 (0)