Skip to content

ref(utils): Simplify normalization code and be more specific when erroring #4761

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 7 commits into from
Mar 25, 2022

Conversation

lobsterkatie
Copy link
Member

@lobsterkatie lobsterkatie commented Mar 23, 2022

Currently, if an error occurs anywhere in the normalization process, the entire value is clobbered, in favor of the string "**non-serializable**". This has a few drawbacks:

  • All data is lost, even data that wasn't malformed
  • It's harder to tell where the problem is, because there's no way to know which part of the data caused the problem
  • It's possible to pass normalize an object and get back a string, which can prove problematic if data is later added to the normalized property as if it were still an object (see the issue linked below).

The goal of this PR, then, was to identify all of the places where errors could occur and catch them right there, rather than relying on the try-catch in the main normalize function. In order to make this easier to reason about, the normalization code was first streamlined a bit. Key changes:

  • serializeValue and makeSerializable have been combined into a single function, stringifyValue, and any code not resulting in a string has been moved into the calling function.
  • All code in stringifyValue has been wrapped in a try-catch, so that errors which originate there will only affect the value at hand, rather than then entire object.
  • When "**non-serializable**" has to be used, it's now accompanied by the message belonging to the error which broke the serialization.
  • When the outer normalize function needs to return "**non-serializable**", it now wraps it in an object, so that other data can still be added.
  • The walk function has been renamed visit, to better conform to the language of the Visitor Pattern. For the moment, it's still aliased to walk when exported, to preserve backwards compatibility.
  • The getWalkSource function has been renamed convertToPlainObject, to better reflect what it does, and some of its repetitive code has been extracted into the serializeEventTarget and getOwnProperties functions.

Fixes #4719

Ref: https://getsentry.atlassian.net/browse/WEB-703

@github-actions
Copy link
Contributor

github-actions bot commented Mar 23, 2022

size-limit report

Path Base Size (437d7cd) Current Size Change
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.58 KB 19.62 KB +0.18% 🔺
@sentry/browser - ES5 CDN Bundle (minified) 62.46 KB 62.43 KB -0.06% 🔽
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 18.19 KB 18.28 KB +0.49% 🔺
@sentry/browser - ES6 CDN Bundle (minified) 55.96 KB 55.94 KB -0.03% 🔽
@sentry/browser - Webpack (gzipped + minified) 22.72 KB 22.77 KB +0.21% 🔺
@sentry/browser - Webpack (minified) 80.38 KB 80.4 KB +0.03% 🔺
@sentry/react - Webpack (gzipped + minified) 22.76 KB 22.81 KB +0.22% 🔺
@sentry/nextjs Client - Webpack (gzipped + minified) 47.69 KB 47.75 KB +0.12% 🔺
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 25.46 KB 25.49 KB +0.15% 🔺
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified) 23.81 KB 23.88 KB +0.31% 🔺

@lobsterkatie lobsterkatie force-pushed the kmclb-specify-which-property-is-unserializable branch 2 times, most recently from 0e574e6 to 84e9545 Compare March 24, 2022 00:09
@lobsterkatie lobsterkatie marked this pull request as ready for review March 24, 2022 00:28
@AbhiPrasad
Copy link
Member

cc @timfish

Copy link
Contributor

@lforst lforst left a comment

Choose a reason for hiding this comment

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

This is a great PR. It simplifies a lot! I left some thoughts (and maybe a bug?) below.

@timfish
Copy link
Collaborator

timfish commented Mar 24, 2022

Looks like @lforst has already give this a comprehensive review but looks like a good improvement!

@lobsterkatie lobsterkatie force-pushed the kmclb-specify-which-property-is-unserializable branch from 84e9545 to 760ca73 Compare March 24, 2022 23:32
@github-actions
Copy link
Contributor

github-actions bot commented Mar 24, 2022

size-limit report 📦

Path Size
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.64 KB (+0.17% 🔺)
@sentry/browser - ES5 CDN Bundle (minified) 62.7 KB (-0.09% 🔽)
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 18.31 KB (+0.55% 🔺)
@sentry/browser - ES6 CDN Bundle (minified) 56.22 KB (-0.06% 🔽)
@sentry/browser - Webpack (gzipped + minified) 22.8 KB (+0.29% 🔺)
@sentry/browser - Webpack (minified) 80.69 KB (+0.02% 🔺)
@sentry/react - Webpack (gzipped + minified) 22.84 KB (+0.3% 🔺)
@sentry/nextjs Client - Webpack (gzipped + minified) 47.77 KB (+0.14% 🔺)
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 25.52 KB (+0.14% 🔺)
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified) 23.92 KB (+0.37% 🔺)

@lobsterkatie lobsterkatie force-pushed the kmclb-specify-which-property-is-unserializable branch from 760ca73 to c8f8cf6 Compare March 25, 2022 01:29
@lobsterkatie lobsterkatie merged commit 5375de8 into master Mar 25, 2022
@lobsterkatie lobsterkatie deleted the kmclb-specify-which-property-is-unserializable branch March 25, 2022 14:51
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.

Issue Cannot create property 'normalizeDepth' on string '**non-serializable**'
4 participants