Skip to content

Commit cdaf852

Browse files
authored
fix(core): Mark stack trace from captureMessage with attatchStackTrace: true as synthetic (#14670)
Analogously to #14668 for browser, this patch marks synthetic exceptions captured during a `captureException()` call as synthetic.
1 parent 825fe89 commit cdaf852

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
2+
import * as Sentry from '@sentry/node';
3+
4+
Sentry.init({
5+
dsn: 'https://[email protected]/1337',
6+
release: '1.0',
7+
transport: loggingTransport,
8+
attachStacktrace: true,
9+
});
10+
11+
Sentry.captureMessage('Message');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { cleanupChildProcesses, createRunner } from '../../../../utils/runner';
2+
3+
afterAll(() => {
4+
cleanupChildProcesses();
5+
});
6+
7+
test('capture a simple message string with a stack trace if `attachStackTrace` is `true`', done => {
8+
createRunner(__dirname, 'scenario.ts')
9+
.expect({
10+
event: {
11+
message: 'Message',
12+
level: 'info',
13+
exception: {
14+
values: [
15+
{
16+
mechanism: { synthetic: true, type: 'generic', handled: true },
17+
value: 'Message',
18+
stacktrace: { frames: expect.any(Array) },
19+
},
20+
],
21+
},
22+
},
23+
})
24+
.start(done);
25+
});

packages/core/src/utils-hoist/eventbuilder.ts

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ export function eventFromMessage(
193193
},
194194
],
195195
};
196+
addExceptionMechanism(event, { synthetic: true });
196197
}
197198
}
198199

packages/core/test/utils-hoist/eventbuilder.test.ts

+61-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Client } from '../../src/types-hoist';
2-
import { eventFromUnknownInput } from '../../src/utils-hoist/eventbuilder';
2+
import { eventFromMessage, eventFromUnknownInput } from '../../src/utils-hoist/eventbuilder';
33
import { nodeStackLineParser } from '../../src/utils-hoist/node-stack-trace';
44
import { createStackParser } from '../../src/utils-hoist/stacktrace';
55

@@ -154,3 +154,63 @@ describe('eventFromUnknownInput', () => {
154154
expect(event.exception?.values?.[0]?.value).toBe('Object captured as exception with keys: foo, prop');
155155
});
156156
});
157+
158+
describe('eventFromMessage', () => {
159+
it('creates an event from a string message', () => {
160+
const event = eventFromMessage(stackParser, 'Test Message');
161+
expect(event).toEqual({
162+
event_id: undefined, // this is undefined because the hint isn't passed
163+
level: 'info',
164+
message: 'Test Message',
165+
});
166+
});
167+
168+
it('attaches a synthetic exception if passed and `attachStackTrace` is true', () => {
169+
const syntheticException = new Error('Test Message');
170+
const event = eventFromMessage(
171+
stackParser,
172+
'Test Message',
173+
'info',
174+
{ syntheticException, event_id: '123abc' },
175+
true,
176+
);
177+
178+
expect(event).toEqual({
179+
event_id: '123abc',
180+
exception: {
181+
values: [
182+
{
183+
mechanism: {
184+
handled: true,
185+
synthetic: true,
186+
type: 'generic',
187+
},
188+
stacktrace: {
189+
frames: expect.any(Array),
190+
},
191+
value: 'Test Message',
192+
},
193+
],
194+
},
195+
level: 'info',
196+
message: 'Test Message',
197+
});
198+
});
199+
200+
it("doesn't attach a synthetic exception if `attachStackTrace` is false", () => {
201+
const syntheticException = new Error('Test Message');
202+
const event = eventFromMessage(
203+
stackParser,
204+
'Test Message',
205+
'info',
206+
{ syntheticException, event_id: '123abc' },
207+
false,
208+
);
209+
210+
expect(event).toEqual({
211+
event_id: '123abc',
212+
level: 'info',
213+
message: 'Test Message',
214+
});
215+
});
216+
});

0 commit comments

Comments
 (0)