1
1
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
2
2
/* eslint-disable max-lines */
3
3
import { getCurrentHub } from '@sentry/core' ;
4
- import type { Event , Integration } from '@sentry/types' ;
4
+ import type { Event as SentryEvent , HandlerDataFetch , Integration , SentryWrappedXMLHttpRequest } from '@sentry/types' ;
5
5
import {
6
6
addInstrumentationHandler ,
7
7
getEventDescription ,
@@ -14,6 +14,8 @@ import {
14
14
15
15
import { WINDOW } from '../helpers' ;
16
16
17
+ type HandlerData = Record < string , unknown > ;
18
+
17
19
/** JSDoc */
18
20
interface BreadcrumbsOptions {
19
21
console : boolean ;
@@ -99,7 +101,7 @@ export class Breadcrumbs implements Integration {
99
101
/**
100
102
* Adds a breadcrumb for Sentry events or transactions if this option is enabled.
101
103
*/
102
- public addSentryBreadcrumb ( event : Event ) : void {
104
+ public addSentryBreadcrumb ( event : SentryEvent ) : void {
103
105
if ( this . options . sentry ) {
104
106
getCurrentHub ( ) . addBreadcrumb (
105
107
{
@@ -120,10 +122,8 @@ export class Breadcrumbs implements Integration {
120
122
* A HOC that creaes a function that creates breadcrumbs from DOM API calls.
121
123
* This is a HOC so that we get access to dom options in the closure.
122
124
*/
123
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
124
- function _domBreadcrumb ( dom : BreadcrumbsOptions [ 'dom' ] ) : ( handlerData : { [ key : string ] : any } ) => void {
125
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
126
- function _innerDomBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
125
+ function _domBreadcrumb ( dom : BreadcrumbsOptions [ 'dom' ] ) : ( handlerData : HandlerData ) => void {
126
+ function _innerDomBreadcrumb ( handlerData : HandlerData ) : void {
127
127
let target ;
128
128
let keyAttrs = typeof dom === 'object' ? dom . serializeAttribute : undefined ;
129
129
@@ -143,9 +143,10 @@ function _domBreadcrumb(dom: BreadcrumbsOptions['dom']): (handlerData: { [key: s
143
143
144
144
// Accessing event.target can throw (see getsentry/raven-js#838, #768)
145
145
try {
146
- target = handlerData . event . target
147
- ? htmlTreeAsString ( handlerData . event . target as Node , { keyAttrs, maxStringLength } )
148
- : htmlTreeAsString ( handlerData . event as unknown as Node , { keyAttrs, maxStringLength } ) ;
146
+ const event = handlerData . event as Event | Node ;
147
+ target = _isEvent ( event )
148
+ ? htmlTreeAsString ( event . target , { keyAttrs, maxStringLength } )
149
+ : htmlTreeAsString ( event , { keyAttrs, maxStringLength } ) ;
149
150
} catch ( e ) {
150
151
target = '<unknown>' ;
151
152
}
@@ -173,8 +174,7 @@ function _domBreadcrumb(dom: BreadcrumbsOptions['dom']): (handlerData: { [key: s
173
174
/**
174
175
* Creates breadcrumbs from console API calls
175
176
*/
176
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
177
- function _consoleBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
177
+ function _consoleBreadcrumb ( handlerData : HandlerData & { args : unknown [ ] ; level : string } ) : void {
178
178
// This is a hack to fix a Vue3-specific bug that causes an infinite loop of
179
179
// console warnings. This happens when a Vue template is rendered with
180
180
// an undeclared variable, which we try to stringify, ultimately causing
@@ -216,8 +216,7 @@ function _consoleBreadcrumb(handlerData: { [key: string]: any }): void {
216
216
/**
217
217
* Creates breadcrumbs from XHR API calls
218
218
*/
219
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
220
- function _xhrBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
219
+ function _xhrBreadcrumb ( handlerData : HandlerData & { xhr : SentryWrappedXMLHttpRequest } ) : void {
221
220
if ( handlerData . endTimestamp ) {
222
221
// We only capture complete, non-sentry requests
223
222
if ( handlerData . xhr . __sentry_own_request__ ) {
@@ -249,8 +248,7 @@ function _xhrBreadcrumb(handlerData: { [key: string]: any }): void {
249
248
/**
250
249
* Creates breadcrumbs from fetch API calls
251
250
*/
252
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
253
- function _fetchBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
251
+ function _fetchBreadcrumb ( handlerData : HandlerData & HandlerDataFetch ) : void {
254
252
// We only capture complete fetch requests
255
253
if ( ! handlerData . endTimestamp ) {
256
254
return ;
@@ -280,7 +278,7 @@ function _fetchBreadcrumb(handlerData: { [key: string]: any }): void {
280
278
category : 'fetch' ,
281
279
data : {
282
280
...handlerData . fetchData ,
283
- status_code : handlerData . response . status ,
281
+ status_code : handlerData . response && handlerData . response . status ,
284
282
} ,
285
283
type : 'http' ,
286
284
} ,
@@ -295,10 +293,9 @@ function _fetchBreadcrumb(handlerData: { [key: string]: any }): void {
295
293
/**
296
294
* Creates breadcrumbs from history API calls
297
295
*/
298
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
299
- function _historyBreadcrumb ( handlerData : { [ key : string ] : any } ) : void {
300
- let from = handlerData . from ;
301
- let to = handlerData . to ;
296
+ function _historyBreadcrumb ( handlerData : HandlerData & { from : string ; to : string } ) : void {
297
+ let from : string | undefined = handlerData . from ;
298
+ let to : string | undefined = handlerData . to ;
302
299
const parsedLoc = parseUrl ( WINDOW . location . href ) ;
303
300
let parsedFrom = parseUrl ( from ) ;
304
301
const parsedTo = parseUrl ( to ) ;
@@ -325,3 +322,7 @@ function _historyBreadcrumb(handlerData: { [key: string]: any }): void {
325
322
} ,
326
323
} ) ;
327
324
}
325
+
326
+ function _isEvent ( event : unknown ) : event is Event {
327
+ return event && ! ! ( event as Record < string , unknown > ) . target ;
328
+ }
0 commit comments