Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Nov 28, 2024
1 parent b9bf16c commit ab39fe8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const btn = document.createElement('button');
btn.id = 'btn';
document.body.appendChild(btn);

const functionListener = function () {
throw new Error('event_listener_error');
};

btn.addEventListener('click', functionListener);

btn.click();
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect } from '@playwright/test';
import type { Event } from '@sentry/types';

import { sentryTest } from '../../../../../utils/fixtures';
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers';

sentryTest('should capture target name in mechanism data', async ({ getLocalTestUrl, page }) => {
const url = await getLocalTestUrl({ testDir: __dirname });

const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);

expect(eventData.exception?.values).toHaveLength(1);
expect(eventData.exception?.values?.[0]).toMatchObject({
type: 'Error',
value: 'event_listener_error',
mechanism: {
type: 'instrument',
handled: false,
data: {
function: 'addEventListener',
handler: 'functionListener',
target: 'EventTarget',
},
},
stacktrace: {
frames: expect.any(Array),
},
});
});
18 changes: 7 additions & 11 deletions packages/browser/src/integrations/browserapierrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,15 @@ function _wrapEventTarget(target: string): void {
const targetObj = globalObject[target];
const proto = targetObj && targetObj.prototype;

if (!proto || Object.prototype.hasOwnProperty.call(proto, 'addEventListener')) {
// eslint-disable-next-line no-prototype-builtins
if (!proto || !proto.hasOwnProperty || !proto.hasOwnProperty('addEventListener')) {
return;
}

fill(proto, 'addEventListener', function (original: VoidFunction,): (
...args: Parameters<typeof WINDOW.addEventListener>
) => void {
return function (
this: unknown,
eventName,
fn,
options,
): (eventName: string, fn: EventListenerObject, capture?: boolean, secure?: boolean) => void {
) => ReturnType<typeof WINDOW.addEventListener> {
return function (this: unknown, eventName, fn, options): VoidFunction {
try {
if (isEventListenerObject(fn)) {
// ESlint disable explanation:
Expand Down Expand Up @@ -222,11 +218,11 @@ function _wrapEventTarget(target: string): void {
};
});

fill(proto, 'removeEventListener', function (originalRemoveEventListener: () => void,): (
fill(proto, 'removeEventListener', function (originalRemoveEventListener: VoidFunction,): (
this: unknown,
...args: Parameters<typeof WINDOW.removeEventListener>
) => () => void {
return function (this: unknown, eventName, fn, options): () => void {
) => ReturnType<typeof WINDOW.removeEventListener> {
return function (this: unknown, eventName, fn, options): VoidFunction {
/**
* There are 2 possible scenarios here:
*
Expand Down

0 comments on commit ab39fe8

Please sign in to comment.