Skip to content

fix(browser): Use normalizeDepth option when creating an event from a plain object #5705

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 2 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/browser/src/eventbuilder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getCurrentHub } from '@sentry/core';
import { Event, EventHint, Exception, Severity, SeverityLevel, StackFrame, StackParser } from '@sentry/types';
import {
addExceptionMechanism,
Expand Down Expand Up @@ -45,6 +46,10 @@ export function eventFromPlainObject(
syntheticException?: Error,
isUnhandledRejection?: boolean,
): Event {
const hub = getCurrentHub();
const client = hub.getClient();
const normalizeDepth = client && client.getOptions().normalizeDepth;

const event: Event = {
exception: {
values: [
Expand All @@ -57,7 +62,7 @@ export function eventFromPlainObject(
],
},
extra: {
__serialized__: normalizeToSize(exception),
__serialized__: normalizeToSize(exception, normalizeDepth),
},
};

Expand Down
64 changes: 64 additions & 0 deletions packages/browser/test/unit/eventbuilder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Client } from '@sentry/types';

import { defaultStackParser } from '../../src';
import { eventFromPlainObject } from '../../src/eventbuilder';

jest.mock('@sentry/core', () => {
const original = jest.requireActual('@sentry/core');
return {
...original,
getCurrentHub(): {
getClient(): Client;
} {
return {
getClient(): any {
return {
getOptions(): any {
return { normalizeDepth: 6 };
},
};
},
};
},
};
});

afterEach(() => {
jest.resetAllMocks();
});

describe('eventFromPlainObject', () => {
it('should use normalizeDepth from init options', () => {
const deepObject = {
a: {
b: {
c: {
d: {
e: {
f: {
g: 'foo',
},
},
},
},
},
},
};

const event = eventFromPlainObject(defaultStackParser, deepObject);

expect(event?.extra?.__serialized__).toEqual({
a: {
b: {
c: {
d: {
e: {
f: '[Object]',
},
},
},
},
},
});
});
});