Skip to content

Commit ab39fe8

Browse files
committed
some fixes
1 parent b9bf16c commit ab39fe8

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const btn = document.createElement('button');
2+
btn.id = 'btn';
3+
document.body.appendChild(btn);
4+
5+
const functionListener = function () {
6+
throw new Error('event_listener_error');
7+
};
8+
9+
btn.addEventListener('click', functionListener);
10+
11+
btn.click();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { expect } from '@playwright/test';
2+
import type { Event } from '@sentry/types';
3+
4+
import { sentryTest } from '../../../../../utils/fixtures';
5+
import { getFirstSentryEnvelopeRequest } from '../../../../../utils/helpers';
6+
7+
sentryTest('should capture target name in mechanism data', async ({ getLocalTestUrl, page }) => {
8+
const url = await getLocalTestUrl({ testDir: __dirname });
9+
10+
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
11+
12+
expect(eventData.exception?.values).toHaveLength(1);
13+
expect(eventData.exception?.values?.[0]).toMatchObject({
14+
type: 'Error',
15+
value: 'event_listener_error',
16+
mechanism: {
17+
type: 'instrument',
18+
handled: false,
19+
data: {
20+
function: 'addEventListener',
21+
handler: 'functionListener',
22+
target: 'EventTarget',
23+
},
24+
},
25+
stacktrace: {
26+
frames: expect.any(Array),
27+
},
28+
});
29+
});

packages/browser/src/integrations/browserapierrors.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,15 @@ function _wrapEventTarget(target: string): void {
167167
const targetObj = globalObject[target];
168168
const proto = targetObj && targetObj.prototype;
169169

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

174175
fill(proto, 'addEventListener', function (original: VoidFunction,): (
175176
...args: Parameters<typeof WINDOW.addEventListener>
176-
) => void {
177-
return function (
178-
this: unknown,
179-
eventName,
180-
fn,
181-
options,
182-
): (eventName: string, fn: EventListenerObject, capture?: boolean, secure?: boolean) => void {
177+
) => ReturnType<typeof WINDOW.addEventListener> {
178+
return function (this: unknown, eventName, fn, options): VoidFunction {
183179
try {
184180
if (isEventListenerObject(fn)) {
185181
// ESlint disable explanation:
@@ -222,11 +218,11 @@ function _wrapEventTarget(target: string): void {
222218
};
223219
});
224220

225-
fill(proto, 'removeEventListener', function (originalRemoveEventListener: () => void,): (
221+
fill(proto, 'removeEventListener', function (originalRemoveEventListener: VoidFunction,): (
226222
this: unknown,
227223
...args: Parameters<typeof WINDOW.removeEventListener>
228-
) => () => void {
229-
return function (this: unknown, eventName, fn, options): () => void {
224+
) => ReturnType<typeof WINDOW.removeEventListener> {
225+
return function (this: unknown, eventName, fn, options): VoidFunction {
230226
/**
231227
* There are 2 possible scenarios here:
232228
*

0 commit comments

Comments
 (0)