Skip to content

fix(core): Prevent exception recapturing #4067

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

Merged
merged 12 commits into from
Oct 21, 2021

Conversation

lobsterkatie
Copy link
Member

@lobsterkatie lobsterkatie commented Oct 15, 2021

When we instrument code to catch exceptions, once we've recorded the error we rethrow it, so that whatever would have happened without us still happens. Sometimes, that means that we'll see the same exception (as in, literally the same instance of Error) again, because it will hit some other code we've instrumented.

The clearest example of this is us catching an exception in, say, an event handler, recording it, rethrowing it, and it then hitting our wrapped global onerror. We already handle that particular case, but the nextjs SDK has introduced other places where this happens, which our current solution doesn't handle.

(Specifically, because we instrument the code in different (but overlapping) ways to compensate for differences between Vercel and non-Vercel environments, in non-Vercel environments we can end up seeing errors get caught twice.)

This prevents us from capturing the same error twice by flagging the object as seen and bailing if we encounter an error with the flag set. Since people do all sorts of crazy things, and throw primitives (especially strings), it also accounts for that case, by wrapping any primitive it encounters with its corresponding wrapper class. Finally, it takes advantage of this change in @sentry/nextjs's withSentry wrapper, to prevent the cases mentioned above.

(Note: This is a different case than the Dedupe integration is meant to handle, which is two copies of the same error in a row. In this case, though the thing which gets thrown is the same the first and second times we see it, the stacktrace to get to it being captured is different, since different instrumentation is involved each time.)

@github-actions
Copy link
Contributor

github-actions bot commented Oct 15, 2021

size-limit report

Path Size
@sentry/browser - CDN Bundle (gzipped) 22.47 KB (+0.4% 🔺)
@sentry/browser - Webpack 23.35 KB (+0.39% 🔺)
@sentry/react - Webpack 23.38 KB (+0.38% 🔺)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 29.91 KB (+0.32% 🔺)

@lobsterkatie lobsterkatie force-pushed the kmclb-prevent-exception-recapturing branch 2 times, most recently from 2b3311e to 9352f76 Compare October 18, 2021 20:42
@lobsterkatie lobsterkatie marked this pull request as ready for review October 19, 2021 05:23
@lobsterkatie lobsterkatie changed the title [WIP] fix(core): Prevent exception recapturing fix(core): Prevent exception recapturing Oct 19, 2021
@lobsterkatie lobsterkatie force-pushed the kmclb-prevent-exception-recapturing branch 4 times, most recently from 5e6ee55 to 35adf41 Compare October 19, 2021 05:52
Copy link
Contributor

@iker-barriocanal iker-barriocanal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushing back because of tests.

*/
export function objectify(wat: unknown): typeof Object {
let objectified;
switch (true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A switch where the condition is always the same value doesn't seem very reasonable to me. Why not if..elif..else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a different (and in some cases, cleaner) way to write a long series of if-elses. Here's a blog post about it: https://seanbarry.dev/posts/switch-true-pattern.

@lobsterkatie lobsterkatie force-pushed the kmclb-prevent-exception-recapturing branch from 35adf41 to 79bbfdd Compare October 19, 2021 18:24
@lobsterkatie
Copy link
Member Author

Thanks for the detailed review! I cleaned up the tests and added comments where it seems like things might have been unclear.

@lobsterkatie lobsterkatie force-pushed the kmclb-prevent-exception-recapturing branch from 79bbfdd to ed18896 Compare October 21, 2021 03:25
@lobsterkatie lobsterkatie force-pushed the kmclb-prevent-exception-recapturing branch from ed18896 to a11c886 Compare October 21, 2021 05:57
@lobsterkatie lobsterkatie merged commit 3a1f7a1 into master Oct 21, 2021
@lobsterkatie lobsterkatie deleted the kmclb-prevent-exception-recapturing branch October 21, 2021 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants