@@ -531,17 +531,22 @@ function instrumentDOM(): void {
531
531
options ?: boolean | AddEventListenerOptions ,
532
532
) : AddEventListener {
533
533
if ( type === 'click' || type == 'keypress' ) {
534
- const el = this as InstrumentedElement ;
535
- const handlers = ( el . __sentry_instrumentation_handlers__ = el . __sentry_instrumentation_handlers__ || { } ) ;
536
- const handlerForType = ( handlers [ type ] = handlers [ type ] || { refCount : 0 } ) ;
537
-
538
- if ( ! handlerForType . handler ) {
539
- const handler = makeDOMEventHandler ( triggerDOMHandler ) ;
540
- handlerForType . handler = handler ;
541
- originalAddEventListener . call ( this , type , handler , options ) ;
542
- }
534
+ try {
535
+ const el = this as InstrumentedElement ;
536
+ const handlers = ( el . __sentry_instrumentation_handlers__ = el . __sentry_instrumentation_handlers__ || { } ) ;
537
+ const handlerForType = ( handlers [ type ] = handlers [ type ] || { refCount : 0 } ) ;
543
538
544
- handlerForType . refCount += 1 ;
539
+ if ( ! handlerForType . handler ) {
540
+ const handler = makeDOMEventHandler ( triggerDOMHandler ) ;
541
+ handlerForType . handler = handler ;
542
+ originalAddEventListener . call ( this , type , handler , options ) ;
543
+ }
544
+
545
+ handlerForType . refCount += 1 ;
546
+ } catch ( e ) {
547
+ // Accessing dom properties is always fragile.
548
+ // Also allows us to skip `addEventListenrs` calls with no proper `this` context.
549
+ }
545
550
}
546
551
547
552
return originalAddEventListener . call ( this , type , listener , options ) ;
@@ -576,7 +581,8 @@ function instrumentDOM(): void {
576
581
}
577
582
}
578
583
} catch ( e ) {
579
- // accessing dom properties is always fragile
584
+ // Accessing dom properties is always fragile.
585
+ // Also allows us to skip `addEventListenrs` calls with no proper `this` context.
580
586
}
581
587
}
582
588
0 commit comments