Skip to content

Commit

Permalink
fix(browser): Mark stack trace from captureMessage with `attachStac…
Browse files Browse the repository at this point in the history
…ktrace: true` as synthetic (#14668)

Add the `synthetic: true` flag to the `mechanism` of a
synthetic exception captured alongside a `captureMessage` call. Setting
this property marks the exception as synthetic for processing on the backend
  • Loading branch information
Lms24 authored Dec 12, 2024
1 parent 4871906 commit 825fe89
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://[email protected]/1337',
attachStacktrace: true,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sentry.captureMessage('foo');
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect } from '@playwright/test';
import type { Event } from '@sentry/core';

import { sentryTest } from '../../../../utils/fixtures';
import { getFirstSentryEnvelopeRequest } from '../../../../utils/helpers';

sentryTest(
'captures a simple message string with stack trace if `attachStackTrace` is `true`',
async ({ getLocalTestUrl, page }) => {
const url = await getLocalTestUrl({ testDir: __dirname });

const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);

expect(eventData.message).toBe('foo');
expect(eventData.level).toBe('info');
expect(eventData.exception?.values?.[0]).toEqual({
mechanism: {
handled: true,
type: 'generic',
synthetic: true,
},
stacktrace: {
frames: expect.arrayContaining([expect.any(Object), expect.any(Object)]),
},
value: 'foo',
});
},
);
1 change: 1 addition & 0 deletions packages/browser/src/eventbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ function eventFromString(
values: [{ value: message, stacktrace: { frames } }],
};
}
addExceptionMechanism(event, { synthetic: true });
}

if (isParameterizedString(message)) {
Expand Down
32 changes: 31 additions & 1 deletion packages/browser/test/eventbuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { afterEach, describe, expect, it, vi } from 'vitest';

import { defaultStackParser } from '../src';
import { eventFromUnknownInput, extractMessage, extractType } from '../src/eventbuilder';
import { eventFromMessage, eventFromUnknownInput, extractMessage, extractType } from '../src/eventbuilder';

vi.mock('@sentry/core', async requireActual => {
return {
Expand Down Expand Up @@ -231,3 +231,33 @@ describe('extractName', () => {
expect(name).toBeUndefined();
});
});

describe('eventFromMessage ', () => {
it('creates an event from a string message', async () => {
const event = await eventFromMessage(defaultStackParser, 'Test message');
expect(event).toEqual({
level: 'info',
message: 'Test message',
});
});

it('creates an event with a synthetic stack trace if attachStacktrace is true', async () => {
const syntheticException = new Error('Test message');
const event = await eventFromMessage(defaultStackParser, 'Test message', 'info', { syntheticException }, true);
expect(event.exception?.values?.[0]).toEqual(
expect.objectContaining({
mechanism: { handled: true, synthetic: true, type: 'generic' },
stacktrace: {
frames: expect.arrayContaining([expect.any(Object), expect.any(Object)]),
},
value: 'Test message',
}),
);
});

it("doesn't add a synthetic stack trace if attachStacktrace is false, even if one is passed-", async () => {
const syntheticException = new Error('Test message');
const event = await eventFromMessage(defaultStackParser, 'Test message', 'info', { syntheticException }, false);
expect(event.exception).toBeUndefined();
});
});

0 comments on commit 825fe89

Please sign in to comment.