Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(browser): Mark stack trace from captureMessage with attachStacktrace: true as synthetic #14668

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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://public@dsn.ingest.sentry.io/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();
});
});
Loading