Skip to content

Commit 18871c6

Browse files
committed
Don't break when function call context is undefined
1 parent 3456d70 commit 18871c6

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

packages/utils/src/instrument.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -531,17 +531,22 @@ function instrumentDOM(): void {
531531
options?: boolean | AddEventListenerOptions,
532532
): AddEventListener {
533533
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 });
543538

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+
}
545550
}
546551

547552
return originalAddEventListener.call(this, type, listener, options);
@@ -576,7 +581,8 @@ function instrumentDOM(): void {
576581
}
577582
}
578583
} 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.
580586
}
581587
}
582588

0 commit comments

Comments
 (0)