Skip to content
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(normalize): Treat Infinity as NaN both are non-serializable numbers #13406

Merged
merged 9 commits into from
Sep 12, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ sentryTest('should set extras from multiple consecutive calls', async ({ getLoca
const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);

expect(eventData.message).toBe('consecutive_calls');
expect(eventData.extra).toMatchObject({ extra: [], Infinity: 2, null: null, obj: { foo: ['bar', 'baz', 1] } });
Copy link
Member Author

Choose a reason for hiding this comment

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

The value under null key is Infinity

expect(eventData.extra).toMatchObject({
extra: [],
Infinity: 2,
null: '[Infinity]',
obj: { foo: ['bar', 'baz', 1] },
});
});
7 changes: 4 additions & 3 deletions packages/utils/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ function visit(
// Get the simple cases out of the way first
if (
value == null || // this matches null and undefined -> eqeq not eqeqeq
(['number', 'boolean', 'string'].includes(typeof value) && !Number.isNaN(value))
['boolean', 'string'].includes(typeof value) ||
(typeof value === 'number' && Number.isFinite(value))
) {
return value as Primitive;
}
Expand Down Expand Up @@ -220,8 +221,8 @@ function stringifyValue(
return '[SyntheticEvent]';
}

if (typeof value === 'number' && value !== value) {
return '[NaN]';
if (typeof value === 'number' && !Number.isFinite(value)) {
Copy link
Member

Choose a reason for hiding this comment

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

actually, we can improve this even further :D we can combine this with the check above for nan, as (NaN).toString() also returns NaN. And isFinite(NaN) === false. So we can remove this completely:

if (typeof value === 'number' && value !== value) {
      return '[NaN]';
    }

and only keep the new check you added!

return `[${value}]`;
}

if (typeof value === 'function') {
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/test/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ describe('normalize()', () => {
describe('changes unserializeable/global values/classes to their respective string representations', () => {
test('primitive values', () => {
expect(normalize(NaN)).toEqual('[NaN]');
expect(normalize(Infinity)).toEqual('[Infinity]');
expect(normalize(-Infinity)).toEqual('[-Infinity]');
expect(normalize(Symbol('dogs'))).toEqual('[Symbol(dogs)]');
expect(normalize(BigInt(1121201212312012))).toEqual('[BigInt: 1121201212312012]');
});
Expand Down
Loading