Skip to content

Commit bfc2d34

Browse files
AbhiPrasadmitsuhiko
authored andcommitted
ref(browser): Extract private methods from breadcrumbs (#4296)
Co-authored-by: Armin Ronacher <[email protected]>
1 parent cd85a5c commit bfc2d34

File tree

1 file changed

+143
-147
lines changed

1 file changed

+143
-147
lines changed

packages/browser/src/integrations/breadcrumbs.ts

+143-147
Original file line numberDiff line numberDiff line change
@@ -85,84 +85,47 @@ export class Breadcrumbs implements Integration {
8585
public setupOnce(): void {
8686
if (this._options.console) {
8787
addInstrumentationHandler({
88-
callback: (...args) => {
89-
this._consoleBreadcrumb(...args);
90-
},
88+
callback: _consoleBreadcrumb,
9189
type: 'console',
9290
});
9391
}
9492
if (this._options.dom) {
9593
addInstrumentationHandler({
96-
callback: (...args) => {
97-
this._domBreadcrumb(...args);
98-
},
94+
callback: _domBreadcrumb(this._options.dom),
9995
type: 'dom',
10096
});
10197
}
10298
if (this._options.xhr) {
10399
addInstrumentationHandler({
104-
callback: (...args) => {
105-
this._xhrBreadcrumb(...args);
106-
},
100+
callback: _xhrBreadcrumb,
107101
type: 'xhr',
108102
});
109103
}
110104
if (this._options.fetch) {
111105
addInstrumentationHandler({
112-
callback: (...args) => {
113-
this._fetchBreadcrumb(...args);
114-
},
106+
callback: _fetchBreadcrumb,
115107
type: 'fetch',
116108
});
117109
}
118110
if (this._options.history) {
119111
addInstrumentationHandler({
120-
callback: (...args) => {
121-
this._historyBreadcrumb(...args);
122-
},
112+
callback: _historyBreadcrumb,
123113
type: 'history',
124114
});
125115
}
126116
}
117+
}
127118

128-
/**
129-
* Creates breadcrumbs from console API calls
130-
*/
131-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
132-
private _consoleBreadcrumb(handlerData: { [key: string]: any }): void {
133-
const breadcrumb = {
134-
category: 'console',
135-
data: {
136-
arguments: handlerData.args,
137-
logger: 'console',
138-
},
139-
level: Severity.fromString(handlerData.level),
140-
message: safeJoin(handlerData.args, ' '),
141-
};
142-
143-
if (handlerData.level === 'assert') {
144-
if (handlerData.args[0] === false) {
145-
breadcrumb.message = `Assertion failed: ${safeJoin(handlerData.args.slice(1), ' ') || 'console.assert'}`;
146-
breadcrumb.data.arguments = handlerData.args.slice(1);
147-
} else {
148-
// Don't capture a breadcrumb for passed assertions
149-
return;
150-
}
151-
}
152-
153-
getCurrentHub().addBreadcrumb(breadcrumb, {
154-
input: handlerData.args,
155-
level: handlerData.level,
156-
});
157-
}
158-
159-
/**
160-
* Creates breadcrumbs from DOM API calls
161-
*/
119+
/**
120+
* A HOC that creaes a function that creates breadcrumbs from DOM API calls.
121+
* This is a HOC so that we get access to dom options in the closure.
122+
*/
123+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
124+
function _domBreadcrumb(dom: BreadcrumbsOptions['dom']): (handlerData: { [key: string]: any }) => void {
162125
// eslint-disable-next-line @typescript-eslint/no-explicit-any
163-
private _domBreadcrumb(handlerData: { [key: string]: any }): void {
126+
function _innerDomBreadcrumb(handlerData: { [key: string]: any }): void {
164127
let target;
165-
let keyAttrs = typeof this._options.dom === 'object' ? this._options.dom.serializeAttribute : undefined;
128+
let keyAttrs = typeof dom === 'object' ? dom.serializeAttribute : undefined;
166129

167130
if (typeof keyAttrs === 'string') {
168131
keyAttrs = [keyAttrs];
@@ -194,117 +157,150 @@ export class Breadcrumbs implements Integration {
194157
);
195158
}
196159

197-
/**
198-
* Creates breadcrumbs from XHR API calls
199-
*/
200-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
201-
private _xhrBreadcrumb(handlerData: { [key: string]: any }): void {
202-
if (handlerData.endTimestamp) {
203-
// We only capture complete, non-sentry requests
204-
if (handlerData.xhr.__sentry_own_request__) {
205-
return;
206-
}
207-
208-
const { method, url, status_code, body } = handlerData.xhr.__sentry_xhr__ || {};
160+
return _innerDomBreadcrumb;
161+
}
209162

210-
getCurrentHub().addBreadcrumb(
211-
{
212-
category: 'xhr',
213-
data: {
214-
method,
215-
url,
216-
status_code,
217-
},
218-
type: 'http',
219-
},
220-
{
221-
xhr: handlerData.xhr,
222-
input: body,
223-
},
224-
);
163+
/**
164+
* Creates breadcrumbs from console API calls
165+
*/
166+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
167+
function _consoleBreadcrumb(handlerData: { [key: string]: any }): void {
168+
const breadcrumb = {
169+
category: 'console',
170+
data: {
171+
arguments: handlerData.args,
172+
logger: 'console',
173+
},
174+
level: Severity.fromString(handlerData.level),
175+
message: safeJoin(handlerData.args, ' '),
176+
};
225177

178+
if (handlerData.level === 'assert') {
179+
if (handlerData.args[0] === false) {
180+
breadcrumb.message = `Assertion failed: ${safeJoin(handlerData.args.slice(1), ' ') || 'console.assert'}`;
181+
breadcrumb.data.arguments = handlerData.args.slice(1);
182+
} else {
183+
// Don't capture a breadcrumb for passed assertions
226184
return;
227185
}
228186
}
229187

230-
/**
231-
* Creates breadcrumbs from fetch API calls
232-
*/
233-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
234-
private _fetchBreadcrumb(handlerData: { [key: string]: any }): void {
235-
// We only capture complete fetch requests
236-
if (!handlerData.endTimestamp) {
237-
return;
238-
}
188+
getCurrentHub().addBreadcrumb(breadcrumb, {
189+
input: handlerData.args,
190+
level: handlerData.level,
191+
});
192+
}
239193

240-
if (handlerData.fetchData.url.match(/sentry_key/) && handlerData.fetchData.method === 'POST') {
241-
// We will not create breadcrumbs for fetch requests that contain `sentry_key` (internal sentry requests)
194+
/**
195+
* Creates breadcrumbs from XHR API calls
196+
*/
197+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
198+
function _xhrBreadcrumb(handlerData: { [key: string]: any }): void {
199+
if (handlerData.endTimestamp) {
200+
// We only capture complete, non-sentry requests
201+
if (handlerData.xhr.__sentry_own_request__) {
242202
return;
243203
}
244204

245-
if (handlerData.error) {
246-
getCurrentHub().addBreadcrumb(
247-
{
248-
category: 'fetch',
249-
data: handlerData.fetchData,
250-
level: Severity.Error,
251-
type: 'http',
252-
},
253-
{
254-
data: handlerData.error,
255-
input: handlerData.args,
256-
},
257-
);
258-
} else {
259-
getCurrentHub().addBreadcrumb(
260-
{
261-
category: 'fetch',
262-
data: {
263-
...handlerData.fetchData,
264-
status_code: handlerData.response.status,
265-
},
266-
type: 'http',
267-
},
268-
{
269-
input: handlerData.args,
270-
response: handlerData.response,
205+
const { method, url, status_code, body } = handlerData.xhr.__sentry_xhr__ || {};
206+
207+
getCurrentHub().addBreadcrumb(
208+
{
209+
category: 'xhr',
210+
data: {
211+
method,
212+
url,
213+
status_code,
271214
},
272-
);
273-
}
274-
}
215+
type: 'http',
216+
},
217+
{
218+
xhr: handlerData.xhr,
219+
input: body,
220+
},
221+
);
275222

276-
/**
277-
* Creates breadcrumbs from history API calls
278-
*/
279-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
280-
private _historyBreadcrumb(handlerData: { [key: string]: any }): void {
281-
const global = getGlobalObject<Window>();
282-
let from = handlerData.from;
283-
let to = handlerData.to;
284-
const parsedLoc = parseUrl(global.location.href);
285-
let parsedFrom = parseUrl(from);
286-
const parsedTo = parseUrl(to);
223+
return;
224+
}
225+
}
287226

288-
// Initial pushState doesn't provide `from` information
289-
if (!parsedFrom.path) {
290-
parsedFrom = parsedLoc;
291-
}
227+
/**
228+
* Creates breadcrumbs from fetch API calls
229+
*/
230+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
231+
function _fetchBreadcrumb(handlerData: { [key: string]: any }): void {
232+
// We only capture complete fetch requests
233+
if (!handlerData.endTimestamp) {
234+
return;
235+
}
292236

293-
// Use only the path component of the URL if the URL matches the current
294-
// document (almost all the time when using pushState)
295-
if (parsedLoc.protocol === parsedTo.protocol && parsedLoc.host === parsedTo.host) {
296-
to = parsedTo.relative;
297-
}
298-
if (parsedLoc.protocol === parsedFrom.protocol && parsedLoc.host === parsedFrom.host) {
299-
from = parsedFrom.relative;
300-
}
237+
if (handlerData.fetchData.url.match(/sentry_key/) && handlerData.fetchData.method === 'POST') {
238+
// We will not create breadcrumbs for fetch requests that contain `sentry_key` (internal sentry requests)
239+
return;
240+
}
301241

302-
getCurrentHub().addBreadcrumb({
303-
category: 'navigation',
304-
data: {
305-
from,
306-
to,
242+
if (handlerData.error) {
243+
getCurrentHub().addBreadcrumb(
244+
{
245+
category: 'fetch',
246+
data: handlerData.fetchData,
247+
level: Severity.Error,
248+
type: 'http',
249+
},
250+
{
251+
data: handlerData.error,
252+
input: handlerData.args,
307253
},
308-
});
254+
);
255+
} else {
256+
getCurrentHub().addBreadcrumb(
257+
{
258+
category: 'fetch',
259+
data: {
260+
...handlerData.fetchData,
261+
status_code: handlerData.response.status,
262+
},
263+
type: 'http',
264+
},
265+
{
266+
input: handlerData.args,
267+
response: handlerData.response,
268+
},
269+
);
309270
}
310271
}
272+
273+
/**
274+
* Creates breadcrumbs from history API calls
275+
*/
276+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
277+
function _historyBreadcrumb(handlerData: { [key: string]: any }): void {
278+
const global = getGlobalObject<Window>();
279+
let from = handlerData.from;
280+
let to = handlerData.to;
281+
const parsedLoc = parseUrl(global.location.href);
282+
let parsedFrom = parseUrl(from);
283+
const parsedTo = parseUrl(to);
284+
285+
// Initial pushState doesn't provide `from` information
286+
if (!parsedFrom.path) {
287+
parsedFrom = parsedLoc;
288+
}
289+
290+
// Use only the path component of the URL if the URL matches the current
291+
// document (almost all the time when using pushState)
292+
if (parsedLoc.protocol === parsedTo.protocol && parsedLoc.host === parsedTo.host) {
293+
to = parsedTo.relative;
294+
}
295+
if (parsedLoc.protocol === parsedFrom.protocol && parsedLoc.host === parsedFrom.host) {
296+
from = parsedFrom.relative;
297+
}
298+
299+
getCurrentHub().addBreadcrumb({
300+
category: 'navigation',
301+
data: {
302+
from,
303+
to,
304+
},
305+
});
306+
}

0 commit comments

Comments
 (0)