-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(utils): Try-catch monkeypatching to handle frozen objects/functions #9031
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
packages/browser-integration-tests/suites/public-api/instrumentation/setTimeout/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function callback() { | ||
throw new Error('setTimeout_error'); | ||
} | ||
|
||
setTimeout(callback, 0); |
24 changes: 24 additions & 0 deletions
24
packages/browser-integration-tests/suites/public-api/instrumentation/setTimeout/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../utils/helpers'; | ||
|
||
sentryTest('Instrumentation should capture errors in setTimeout', async ({ getLocalTestPath, page }) => { | ||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
expect(eventData.exception?.values).toHaveLength(1); | ||
expect(eventData.exception?.values?.[0]).toMatchObject({ | ||
type: 'Error', | ||
value: 'setTimeout_error', | ||
mechanism: { | ||
type: 'instrument', | ||
handled: false, | ||
}, | ||
stacktrace: { | ||
frames: expect.any(Array), | ||
}, | ||
}); | ||
}); |
8 changes: 8 additions & 0 deletions
8
...ages/browser-integration-tests/suites/public-api/instrumentation/setTimeoutFrozen/init.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
debug: true, | ||
}); |
5 changes: 5 additions & 0 deletions
5
...s/browser-integration-tests/suites/public-api/instrumentation/setTimeoutFrozen/subject.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function callback() { | ||
throw new Error('setTimeout_error'); | ||
} | ||
|
||
setTimeout(Object.freeze(callback), 0); |
48 changes: 48 additions & 0 deletions
48
...ages/browser-integration-tests/suites/public-api/instrumentation/setTimeoutFrozen/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { expect } from '@playwright/test'; | ||
import type { Event } from '@sentry/types'; | ||
|
||
import { sentryTest } from '../../../../utils/fixtures'; | ||
import { getFirstSentryEnvelopeRequest } from '../../../../utils/helpers'; | ||
|
||
sentryTest( | ||
'Instrumentation does not fail when using frozen callback for setTimeout', | ||
async ({ getLocalTestPath, page }) => { | ||
const bundleKey = process.env.PW_BUNDLE || ''; | ||
const hasDebug = !bundleKey.includes('_min'); | ||
|
||
const url = await getLocalTestPath({ testDir: __dirname }); | ||
|
||
const logMessages: string[] = []; | ||
|
||
page.on('console', msg => { | ||
logMessages.push(msg.text()); | ||
}); | ||
|
||
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
||
// It still captures the error | ||
expect(eventData.exception?.values).toHaveLength(1); | ||
expect(eventData.exception?.values?.[0]).toMatchObject({ | ||
type: 'Error', | ||
value: 'setTimeout_error', | ||
mechanism: { | ||
type: 'instrument', | ||
handled: false, | ||
}, | ||
stacktrace: { | ||
frames: expect.any(Array), | ||
}, | ||
}); | ||
|
||
// We only care about the message when debug is enabled | ||
if (hasDebug) { | ||
expect(logMessages).toEqual( | ||
expect.arrayContaining([ | ||
expect.stringContaining( | ||
'Sentry Logger [log]: Failed to add non-enumerable property "__sentry_wrapped__" to object function callback()', | ||
), | ||
]), | ||
); | ||
} | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
h: I would add a log here (maybe even non-debug!). I can already see myself debugging an issue that some instrumentation isn't working. This is a very opaque way to fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add a debug log, but wouldn't do a log. Because it is really not a user problem, the linked issue:
Is generally totally OK to write but would result in this. the user shouldn't see a log because of this IMHO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug log sounds good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a debug log & integration tests to actually make sure the issue raised is fixed. Interestingly, if the function if frozen the actual monkey patching still works, only the setting of the wrapped property fails. So the error is still captured even if the callback is frozen, which is good I guess.