Skip to content

Commit ca34264

Browse files
committed
add log & tests
1 parent b96fbbf commit ca34264

File tree

6 files changed

+88
-1
lines changed

6 files changed

+88
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function callback() {
2+
throw new Error('setTimeout_error');
3+
}
4+
5+
setTimeout(callback, 0);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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('Instrumentation should capture errors in setTimeout', async ({ getLocalTestPath, page }) => {
8+
const url = await getLocalTestPath({ 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: 'setTimeout_error',
16+
mechanism: {
17+
type: 'instrument',
18+
handled: false,
19+
},
20+
stacktrace: {
21+
frames: expect.any(Array),
22+
},
23+
});
24+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
debug: true,
8+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function callback() {
2+
throw new Error('setTimeout_error');
3+
}
4+
5+
setTimeout(Object.freeze(callback), 0);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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(
8+
'Instrumentation does not fail when using frozen callback for setTimeout',
9+
async ({ getLocalTestPath, page }) => {
10+
const url = await getLocalTestPath({ testDir: __dirname });
11+
12+
const logMessages: string[] = [];
13+
14+
page.on('console', msg => {
15+
logMessages.push(msg.text());
16+
});
17+
18+
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);
19+
20+
// It still captures the error
21+
expect(eventData.exception?.values).toHaveLength(1);
22+
expect(eventData.exception?.values?.[0]).toMatchObject({
23+
type: 'Error',
24+
value: 'setTimeout_error',
25+
mechanism: {
26+
type: 'instrument',
27+
handled: false,
28+
},
29+
stacktrace: {
30+
frames: expect.any(Array),
31+
},
32+
});
33+
34+
expect(logMessages).toEqual(
35+
expect.arrayContaining([
36+
expect.stringContaining(
37+
'Sentry Logger [log]: Failed to add non-enumerable property "__sentry_wrapped__" to object function callback()',
38+
),
39+
]),
40+
);
41+
},
42+
);

packages/utils/src/object.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { WrappedFunction } from '@sentry/types';
44

55
import { htmlTreeAsString } from './browser';
66
import { isElement, isError, isEvent, isInstanceOf, isPlainObject, isPrimitive } from './is';
7+
import { logger } from './logger';
78
import { truncate } from './string';
89

910
/**
@@ -49,7 +50,9 @@ export function addNonEnumerableProperty(obj: { [key: string]: unknown }, name:
4950
writable: true,
5051
configurable: true,
5152
});
52-
} catch (o_O) {} // eslint-disable-line no-empty
53+
} catch (o_O) {
54+
__DEBUG_BUILD__ && logger.log(`Failed to add non-enumerable property "${name}" to object`, obj);
55+
}
5356
}
5457

5558
/**

0 commit comments

Comments
 (0)