From 08e22c9a72793c720d43a068e967870d3f3fcd75 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 2 Sep 2022 15:15:49 +0200 Subject: [PATCH 1/3] fix(node): Use normalizeDepth option when creating events from unknown input --- packages/node/src/client.ts | 4 +++- packages/node/src/eventbuilder.ts | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/node/src/client.ts b/packages/node/src/client.ts index d8a98ee3bfd5..2bb65db9094a 100644 --- a/packages/node/src/client.ts +++ b/packages/node/src/client.ts @@ -118,7 +118,9 @@ export class NodeClient extends BaseClient { */ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types public eventFromException(exception: any, hint?: EventHint): PromiseLike { - return resolvedSyncPromise(eventFromUnknownInput(this._options.stackParser, exception, hint)); + return resolvedSyncPromise( + eventFromUnknownInput(this._options.stackParser, exception, hint, this._options.normalizeDepth), + ); } /** diff --git a/packages/node/src/eventbuilder.ts b/packages/node/src/eventbuilder.ts index eead089793a0..ece1af9f772c 100644 --- a/packages/node/src/eventbuilder.ts +++ b/packages/node/src/eventbuilder.ts @@ -46,7 +46,12 @@ export function exceptionFromError(stackParser: StackParser, error: Error): Exce * Builds and Event from a Exception * @hidden */ -export function eventFromUnknownInput(stackParser: StackParser, exception: unknown, hint?: EventHint): Event { +export function eventFromUnknownInput( + stackParser: StackParser, + exception: unknown, + hint?: EventHint, + normalizeDepth?: number, +): Event { // eslint-disable-next-line @typescript-eslint/no-explicit-any let ex: unknown = exception; const providedMechanism: Mechanism | undefined = @@ -63,7 +68,7 @@ export function eventFromUnknownInput(stackParser: StackParser, exception: unkno const message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(exception)}`; getCurrentHub().configureScope(scope => { - scope.setExtra('__serialized__', normalizeToSize(exception)); + scope.setExtra('__serialized__', normalizeToSize(exception, normalizeDepth)); }); ex = (hint && hint.syntheticException) || new Error(message); From 8ebddd4c63fb07c2a2a72f33e068f0bb3f0d748a Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 2 Sep 2022 15:47:39 +0200 Subject: [PATCH 2/3] add test --- packages/node/src/client.ts | 4 +- packages/node/src/eventbuilder.ts | 12 ++-- packages/node/test/eventbuilders.test.ts | 75 ++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 packages/node/test/eventbuilders.test.ts diff --git a/packages/node/src/client.ts b/packages/node/src/client.ts index 2bb65db9094a..d8a98ee3bfd5 100644 --- a/packages/node/src/client.ts +++ b/packages/node/src/client.ts @@ -118,9 +118,7 @@ export class NodeClient extends BaseClient { */ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types public eventFromException(exception: any, hint?: EventHint): PromiseLike { - return resolvedSyncPromise( - eventFromUnknownInput(this._options.stackParser, exception, hint, this._options.normalizeDepth), - ); + return resolvedSyncPromise(eventFromUnknownInput(this._options.stackParser, exception, hint)); } /** diff --git a/packages/node/src/eventbuilder.ts b/packages/node/src/eventbuilder.ts index ece1af9f772c..c6fdeb0598e2 100644 --- a/packages/node/src/eventbuilder.ts +++ b/packages/node/src/eventbuilder.ts @@ -46,12 +46,7 @@ export function exceptionFromError(stackParser: StackParser, error: Error): Exce * Builds and Event from a Exception * @hidden */ -export function eventFromUnknownInput( - stackParser: StackParser, - exception: unknown, - hint?: EventHint, - normalizeDepth?: number, -): Event { +export function eventFromUnknownInput(stackParser: StackParser, exception: unknown, hint?: EventHint): Event { // eslint-disable-next-line @typescript-eslint/no-explicit-any let ex: unknown = exception; const providedMechanism: Mechanism | undefined = @@ -67,7 +62,10 @@ export function eventFromUnknownInput( // which is much better than creating new group when any key/value change const message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(exception)}`; - getCurrentHub().configureScope(scope => { + const hub = getCurrentHub(); + const client = hub.getClient(); + const normalizeDepth = client && client.getOptions().normalizeDepth; + hub.configureScope(scope => { scope.setExtra('__serialized__', normalizeToSize(exception, normalizeDepth)); }); diff --git a/packages/node/test/eventbuilders.test.ts b/packages/node/test/eventbuilders.test.ts new file mode 100644 index 000000000000..abac4b032efa --- /dev/null +++ b/packages/node/test/eventbuilders.test.ts @@ -0,0 +1,75 @@ +import { Client } from '@sentry/types'; +import { defaultStackParser, Scope } from '../src'; +import { eventFromUnknownInput } from '../src/eventbuilder'; + +const testScope = new Scope(); + +jest.mock('@sentry/hub', () => { + const original = jest.requireActual('@sentry/hub'); + return { + ...original, + getCurrentHub(): { + getClient(): Client; + getScope(): Scope; + configureScope(scopeFunction: (scope: Scope) => void): void; + } { + return { + getClient(): any { + return { + getOptions(): any { + return { normalizeDepth: 6 }; + }, + }; + }, + getScope(): Scope { + return new Scope(); + }, + configureScope(scopeFunction): void { + scopeFunction(testScope); + }, + }; + }, + }; +}); + +afterEach(() => { + jest.resetAllMocks(); +}); + +describe('eventFromUnknownInput', () => { + test('uses normalizeDepth from init options', () => { + const deepObject = { + a: { + b: { + c: { + d: { + e: { + f: { + g: 'foo', + }, + }, + }, + }, + }, + }, + }; + + eventFromUnknownInput(defaultStackParser, deepObject); + + const serializedObject = (testScope as any)._extra.__serialized__; + expect(serializedObject).toBeDefined(); + expect(serializedObject).toEqual({ + a: { + b: { + c: { + d: { + e: { + f: '[Object]', + }, + }, + }, + }, + }, + }); + }); +}); From 5a80eec583f370c45e176e6602c524cc4a623e95 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Mon, 5 Sep 2022 09:27:18 +0000 Subject: [PATCH 3/3] Please linter --- packages/node/test/eventbuilders.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/node/test/eventbuilders.test.ts b/packages/node/test/eventbuilders.test.ts index abac4b032efa..f92ec303bb3c 100644 --- a/packages/node/test/eventbuilders.test.ts +++ b/packages/node/test/eventbuilders.test.ts @@ -1,4 +1,5 @@ import { Client } from '@sentry/types'; + import { defaultStackParser, Scope } from '../src'; import { eventFromUnknownInput } from '../src/eventbuilder'; @@ -24,7 +25,7 @@ jest.mock('@sentry/hub', () => { getScope(): Scope { return new Scope(); }, - configureScope(scopeFunction): void { + configureScope(scopeFunction: (scope: Scope) => void): void { scopeFunction(testScope); }, };