Skip to content

Commit

Permalink
fix(core): Mark stack trace from captureMessage with `attatchStackT…
Browse files Browse the repository at this point in the history
…race: true` as synthetic (#14670)

Analogously to #14668 for browser, this patch marks synthetic exceptions
captured during a `captureException()` call as synthetic.
  • Loading branch information
Lms24 authored Dec 12, 2024
1 parent 825fe89 commit cdaf852
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
transport: loggingTransport,
attachStacktrace: true,
});

Sentry.captureMessage('Message');
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { cleanupChildProcesses, createRunner } from '../../../../utils/runner';

afterAll(() => {
cleanupChildProcesses();
});

test('capture a simple message string with a stack trace if `attachStackTrace` is `true`', done => {
createRunner(__dirname, 'scenario.ts')
.expect({
event: {
message: 'Message',
level: 'info',
exception: {
values: [
{
mechanism: { synthetic: true, type: 'generic', handled: true },
value: 'Message',
stacktrace: { frames: expect.any(Array) },
},
],
},
},
})
.start(done);
});
1 change: 1 addition & 0 deletions packages/core/src/utils-hoist/eventbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export function eventFromMessage(
},
],
};
addExceptionMechanism(event, { synthetic: true });
}
}

Expand Down
62 changes: 61 additions & 1 deletion packages/core/test/utils-hoist/eventbuilder.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Client } from '../../src/types-hoist';
import { eventFromUnknownInput } from '../../src/utils-hoist/eventbuilder';
import { eventFromMessage, eventFromUnknownInput } from '../../src/utils-hoist/eventbuilder';
import { nodeStackLineParser } from '../../src/utils-hoist/node-stack-trace';
import { createStackParser } from '../../src/utils-hoist/stacktrace';

Expand Down Expand Up @@ -154,3 +154,63 @@ describe('eventFromUnknownInput', () => {
expect(event.exception?.values?.[0]?.value).toBe('Object captured as exception with keys: foo, prop');
});
});

describe('eventFromMessage', () => {
it('creates an event from a string message', () => {
const event = eventFromMessage(stackParser, 'Test Message');
expect(event).toEqual({
event_id: undefined, // this is undefined because the hint isn't passed
level: 'info',
message: 'Test Message',
});
});

it('attaches a synthetic exception if passed and `attachStackTrace` is true', () => {
const syntheticException = new Error('Test Message');
const event = eventFromMessage(
stackParser,
'Test Message',
'info',
{ syntheticException, event_id: '123abc' },
true,
);

expect(event).toEqual({
event_id: '123abc',
exception: {
values: [
{
mechanism: {
handled: true,
synthetic: true,
type: 'generic',
},
stacktrace: {
frames: expect.any(Array),
},
value: 'Test Message',
},
],
},
level: 'info',
message: 'Test Message',
});
});

it("doesn't attach a synthetic exception if `attachStackTrace` is false", () => {
const syntheticException = new Error('Test Message');
const event = eventFromMessage(
stackParser,
'Test Message',
'info',
{ syntheticException, event_id: '123abc' },
false,
);

expect(event).toEqual({
event_id: '123abc',
level: 'info',
message: 'Test Message',
});
});
});

0 comments on commit cdaf852

Please sign in to comment.