From 2c4005ae851829906d2b32ad4dff7fa7916d2bb5 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 11 Dec 2024 17:22:48 +0100 Subject: [PATCH 1/4] fix(browser): Mark stack trace from `captureMessage` with `attatchStackTrace: true` as synthetic --- .../simple_message_attachStackTrace/init.js | 8 +++++ .../subject.js | 1 + .../simple_message_attachStackTrace/test.ts | 28 ++++++++++++++++ packages/browser/src/eventbuilder.ts | 9 ++++-- packages/browser/test/eventbuilder.test.ts | 32 ++++++++++++++++++- 5 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 dev-packages/browser-integration-tests/suites/public-api/captureMessage/simple_message_attachStackTrace/init.js create mode 100644 dev-packages/browser-integration-tests/suites/public-api/captureMessage/simple_message_attachStackTrace/subject.js create mode 100644 dev-packages/browser-integration-tests/suites/public-api/captureMessage/simple_message_attachStackTrace/test.ts diff --git a/dev-packages/browser-integration-tests/suites/public-api/captureMessage/simple_message_attachStackTrace/init.js b/dev-packages/browser-integration-tests/suites/public-api/captureMessage/simple_message_attachStackTrace/init.js new file mode 100644 index 000000000000..99ffd9f0ab31 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/captureMessage/simple_message_attachStackTrace/init.js @@ -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, +}); diff --git a/dev-packages/browser-integration-tests/suites/public-api/captureMessage/simple_message_attachStackTrace/subject.js b/dev-packages/browser-integration-tests/suites/public-api/captureMessage/simple_message_attachStackTrace/subject.js new file mode 100644 index 000000000000..cf462c59a2fb --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/captureMessage/simple_message_attachStackTrace/subject.js @@ -0,0 +1 @@ +Sentry.captureMessage('foo'); diff --git a/dev-packages/browser-integration-tests/suites/public-api/captureMessage/simple_message_attachStackTrace/test.ts b/dev-packages/browser-integration-tests/suites/public-api/captureMessage/simple_message_attachStackTrace/test.ts new file mode 100644 index 000000000000..0e769cca73fe --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/captureMessage/simple_message_attachStackTrace/test.ts @@ -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(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', + }); + }, +); diff --git a/packages/browser/src/eventbuilder.ts b/packages/browser/src/eventbuilder.ts index 7f7539f5cf5f..6136a9f7274f 100644 --- a/packages/browser/src/eventbuilder.ts +++ b/packages/browser/src/eventbuilder.ts @@ -252,6 +252,7 @@ export function eventFromMessage( if (hint && hint.event_id) { event.event_id = hint.event_id; } + console.log('xx 3', event, event.exception?.values![0]); return resolvedSyncPromise(event); } @@ -311,6 +312,8 @@ export function eventFromUnknownInput( addExceptionMechanism(event, { synthetic: true, }); + console.log('xx 1', event, event.exception?.values![0]); + return event; } @@ -325,9 +328,8 @@ export function eventFromUnknownInput( // So bail out and capture it as a simple message: event = eventFromString(stackParser, exception as string, syntheticException, attachStacktrace); addExceptionTypeValue(event, `${exception}`, undefined); - addExceptionMechanism(event, { - synthetic: true, - }); + + console.log('xx 2', event, event.exception?.values![0]); return event; } @@ -347,6 +349,7 @@ function eventFromString( values: [{ value: message, stacktrace: { frames } }], }; } + addExceptionMechanism(event, { synthetic: true }); } if (isParameterizedString(message)) { diff --git a/packages/browser/test/eventbuilder.test.ts b/packages/browser/test/eventbuilder.test.ts index 31112abbfc7e..f9949800ee94 100644 --- a/packages/browser/test/eventbuilder.test.ts +++ b/packages/browser/test/eventbuilder.test.ts @@ -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 { @@ -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(); + }); +}); From f01acc803311f81a297642efd79116141d698272 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 11 Dec 2024 17:26:56 +0100 Subject: [PATCH 2/4] cleanup --- packages/browser/src/eventbuilder.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/browser/src/eventbuilder.ts b/packages/browser/src/eventbuilder.ts index 6136a9f7274f..94d4c32090bc 100644 --- a/packages/browser/src/eventbuilder.ts +++ b/packages/browser/src/eventbuilder.ts @@ -252,7 +252,6 @@ export function eventFromMessage( if (hint && hint.event_id) { event.event_id = hint.event_id; } - console.log('xx 3', event, event.exception?.values![0]); return resolvedSyncPromise(event); } @@ -312,8 +311,6 @@ export function eventFromUnknownInput( addExceptionMechanism(event, { synthetic: true, }); - console.log('xx 1', event, event.exception?.values![0]); - return event; } @@ -329,8 +326,6 @@ export function eventFromUnknownInput( event = eventFromString(stackParser, exception as string, syntheticException, attachStacktrace); addExceptionTypeValue(event, `${exception}`, undefined); - console.log('xx 2', event, event.exception?.values![0]); - return event; } From 47b78c7dc41493339b09a34ee8d2346bd7064ec9 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 11 Dec 2024 17:28:33 +0100 Subject: [PATCH 3/4] revert deletion as it wasn't 100% identical --- packages/browser/src/eventbuilder.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/browser/src/eventbuilder.ts b/packages/browser/src/eventbuilder.ts index 94d4c32090bc..ce34be0de707 100644 --- a/packages/browser/src/eventbuilder.ts +++ b/packages/browser/src/eventbuilder.ts @@ -325,6 +325,9 @@ export function eventFromUnknownInput( // So bail out and capture it as a simple message: event = eventFromString(stackParser, exception as string, syntheticException, attachStacktrace); addExceptionTypeValue(event, `${exception}`, undefined); + addExceptionMechanism(event, { + synthetic: true, + }); return event; } From 2ec02c1dda6fa963ec377727e72f138a0c28d6bd Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 11 Dec 2024 18:18:16 +0100 Subject: [PATCH 4/4] bump svelte size limit --- .size-limit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.size-limit.js b/.size-limit.js index 844d57447495..80d56f49bf4b 100644 --- a/.size-limit.js +++ b/.size-limit.js @@ -147,7 +147,7 @@ module.exports = [ path: 'packages/svelte/build/esm/index.js', import: createImport('init'), gzip: true, - limit: '24 KB', + limit: '24.5 KB', }, // Browser CDN bundles {