Skip to content

Commit 8ebddd4

Browse files
committed
add test
1 parent 08e22c9 commit 8ebddd4

File tree

3 files changed

+81
-10
lines changed

3 files changed

+81
-10
lines changed

packages/node/src/client.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
118118
*/
119119
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
120120
public eventFromException(exception: any, hint?: EventHint): PromiseLike<Event> {
121-
return resolvedSyncPromise(
122-
eventFromUnknownInput(this._options.stackParser, exception, hint, this._options.normalizeDepth),
123-
);
121+
return resolvedSyncPromise(eventFromUnknownInput(this._options.stackParser, exception, hint));
124122
}
125123

126124
/**

packages/node/src/eventbuilder.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,7 @@ export function exceptionFromError(stackParser: StackParser, error: Error): Exce
4646
* Builds and Event from a Exception
4747
* @hidden
4848
*/
49-
export function eventFromUnknownInput(
50-
stackParser: StackParser,
51-
exception: unknown,
52-
hint?: EventHint,
53-
normalizeDepth?: number,
54-
): Event {
49+
export function eventFromUnknownInput(stackParser: StackParser, exception: unknown, hint?: EventHint): Event {
5550
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5651
let ex: unknown = exception;
5752
const providedMechanism: Mechanism | undefined =
@@ -67,7 +62,10 @@ export function eventFromUnknownInput(
6762
// which is much better than creating new group when any key/value change
6863
const message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(exception)}`;
6964

70-
getCurrentHub().configureScope(scope => {
65+
const hub = getCurrentHub();
66+
const client = hub.getClient();
67+
const normalizeDepth = client && client.getOptions().normalizeDepth;
68+
hub.configureScope(scope => {
7169
scope.setExtra('__serialized__', normalizeToSize(exception, normalizeDepth));
7270
});
7371

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { Client } from '@sentry/types';
2+
import { defaultStackParser, Scope } from '../src';
3+
import { eventFromUnknownInput } from '../src/eventbuilder';
4+
5+
const testScope = new Scope();
6+
7+
jest.mock('@sentry/hub', () => {
8+
const original = jest.requireActual('@sentry/hub');
9+
return {
10+
...original,
11+
getCurrentHub(): {
12+
getClient(): Client;
13+
getScope(): Scope;
14+
configureScope(scopeFunction: (scope: Scope) => void): void;
15+
} {
16+
return {
17+
getClient(): any {
18+
return {
19+
getOptions(): any {
20+
return { normalizeDepth: 6 };
21+
},
22+
};
23+
},
24+
getScope(): Scope {
25+
return new Scope();
26+
},
27+
configureScope(scopeFunction): void {
28+
scopeFunction(testScope);
29+
},
30+
};
31+
},
32+
};
33+
});
34+
35+
afterEach(() => {
36+
jest.resetAllMocks();
37+
});
38+
39+
describe('eventFromUnknownInput', () => {
40+
test('uses normalizeDepth from init options', () => {
41+
const deepObject = {
42+
a: {
43+
b: {
44+
c: {
45+
d: {
46+
e: {
47+
f: {
48+
g: 'foo',
49+
},
50+
},
51+
},
52+
},
53+
},
54+
},
55+
};
56+
57+
eventFromUnknownInput(defaultStackParser, deepObject);
58+
59+
const serializedObject = (testScope as any)._extra.__serialized__;
60+
expect(serializedObject).toBeDefined();
61+
expect(serializedObject).toEqual({
62+
a: {
63+
b: {
64+
c: {
65+
d: {
66+
e: {
67+
f: '[Object]',
68+
},
69+
},
70+
},
71+
},
72+
},
73+
});
74+
});
75+
});

0 commit comments

Comments
 (0)