From 82082b41ad8b6a8dd706ab5db4e149d12477ab1f Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 29 Aug 2023 16:42:22 +0200 Subject: [PATCH] feat(browser): Mark errors caught from `wrap` as unhandled --- .../suites/public-api/wrap/init.js | 7 +++++++ .../suites/public-api/wrap/subject.js | 3 +++ .../suites/public-api/wrap/test.ts | 21 +++++++++++++++++++ packages/browser/src/sdk.ts | 7 ++++++- 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 packages/browser-integration-tests/suites/public-api/wrap/init.js create mode 100644 packages/browser-integration-tests/suites/public-api/wrap/subject.js create mode 100644 packages/browser-integration-tests/suites/public-api/wrap/test.ts diff --git a/packages/browser-integration-tests/suites/public-api/wrap/init.js b/packages/browser-integration-tests/suites/public-api/wrap/init.js new file mode 100644 index 000000000000..d8c94f36fdd0 --- /dev/null +++ b/packages/browser-integration-tests/suites/public-api/wrap/init.js @@ -0,0 +1,7 @@ +import * as Sentry from '@sentry/browser'; + +window.Sentry = Sentry; + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', +}); diff --git a/packages/browser-integration-tests/suites/public-api/wrap/subject.js b/packages/browser-integration-tests/suites/public-api/wrap/subject.js new file mode 100644 index 000000000000..e7c0f6db282e --- /dev/null +++ b/packages/browser-integration-tests/suites/public-api/wrap/subject.js @@ -0,0 +1,3 @@ +Sentry.wrap(() => { + throw new Error('test'); +}); diff --git a/packages/browser-integration-tests/suites/public-api/wrap/test.ts b/packages/browser-integration-tests/suites/public-api/wrap/test.ts new file mode 100644 index 000000000000..2c5735b670a4 --- /dev/null +++ b/packages/browser-integration-tests/suites/public-api/wrap/test.ts @@ -0,0 +1,21 @@ +import { expect } from '@playwright/test'; +import type { Event } from '@sentry/types'; + +import { sentryTest } from '../../../utils/fixtures'; +import { getFirstSentryEnvelopeRequest } from '../../../utils/helpers'; + +sentryTest('should capture an error within `wrap` and mark it as unhandled', async ({ getLocalTestPath, page }) => { + const url = await getLocalTestPath({ testDir: __dirname }); + + const eventData = await getFirstSentryEnvelopeRequest(page, url); + + expect(eventData.exception?.values).toHaveLength(1); + expect(eventData.exception?.values?.[0]).toMatchObject({ + type: 'Error', + value: 'test', + mechanism: { + type: 'instrument', + handled: false, + }, + }); +}); diff --git a/packages/browser/src/sdk.ts b/packages/browser/src/sdk.ts index 45fd26c75745..e3a4ebb58c45 100644 --- a/packages/browser/src/sdk.ts +++ b/packages/browser/src/sdk.ts @@ -198,7 +198,12 @@ export function onLoad(callback: () => void): void { */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function wrap(fn: (...args: any) => any): any { - return internalWrap(fn)(); + return internalWrap(fn, { + mechanism: { + type: 'instrument', + handled: false, + }, + })(); } function startSessionOnHub(hub: Hub): void {