From 59561e615a62b5930fcf0834010bbb9c3798d620 Mon Sep 17 00:00:00 2001 From: Prince Panchani <77726300+PrinceXDev@users.noreply.github.com> Date: Wed, 12 Aug 2026 18:46:19 +0530 Subject: [PATCH] Add Sentry frame rewriting for Element Desktop integration (#34667) * Add Sentry frame rewriting for Element Desktop integration This change introduces the Sentry `rewriteFramesIntegration` to normalize stack frames for crashes occurring in Element Desktop. It ensures that stack traces from the custom `vector://vector/webapp` origin are grouped and symbolicated correctly, aligning them with the web app's release. This enhancement improves error tracking and debugging for users on the desktop platform. * Refactor Sentry test file for improved readability and consistency This update enhances the formatting of the `sentry-test.ts` file by aligning code for better readability. Changes include consistent indentation and spacing in mock function definitions and test cases. These adjustments aim to improve code maintainability and clarity without altering functionality. * Add copyright notice and SPDX license to Sentry test file This update introduces a copyright notice and SPDX license declaration at the top of the `sentry-test.ts` file. The addition clarifies the ownership and licensing terms for the file, ensuring compliance with legal requirements and improving documentation for future reference. * Update copyright notice in Sentry test file This commit updates the copyright notice in the `sentry-test.ts` file to reflect the new ownership under PrinceXDev. The change ensures that the file's legal attribution is accurate and up-to-date. * Refactor Sentry test file for improved code consistency This commit streamlines the formatting in the `sentry-test.ts` file by consolidating mock function definitions and enhancing the readability of the test cases. The changes include removing unnecessary line breaks and ensuring consistent spacing, which contributes to better maintainability without altering the functionality of the tests. --- apps/web/src/sentry.ts | 8 +++ apps/web/test/unit-tests/sentry-test.ts | 81 +++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 apps/web/test/unit-tests/sentry-test.ts diff --git a/apps/web/src/sentry.ts b/apps/web/src/sentry.ts index d705ef7c02..85565673d7 100644 --- a/apps/web/src/sentry.ts +++ b/apps/web/src/sentry.ts @@ -197,6 +197,14 @@ export async function initSentry(sentryConfig: IConfigOptions["sentry"]): Promis Sentry.breadcrumbsIntegration(), Sentry.httpContextIntegration(), Sentry.dedupeIntegration(), + // Element Desktop serves the app from the custom `vector://vector/webapp` origin rather + // than a normal web origin, so its stack frames don't match Element Web's and end up + // ungrouped and unsymbolicated. Normalize both to the same relative form so desktop + // crashes group with, and source-map against, the same release as the web app. + Sentry.rewriteFramesIntegration({ + root: "vector://vector/webapp", + prefix: "app://", + }), ]; Sentry.init({ diff --git a/apps/web/test/unit-tests/sentry-test.ts b/apps/web/test/unit-tests/sentry-test.ts new file mode 100644 index 0000000000..0c81b577ff --- /dev/null +++ b/apps/web/test/unit-tests/sentry-test.ts @@ -0,0 +1,81 @@ +/* +Copyright 2026 PrinceXDev + +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial +Please see LICENSE files in the repository root for full details. +*/ + +import * as Sentry from "@sentry/browser"; + +import { initSentry } from "../../src/sentry"; + +jest.mock("@sentry/browser", () => ({ + init: jest.fn(), + inboundFiltersIntegration: jest.fn().mockReturnValue({ name: "InboundFilters" }), + functionToStringIntegration: jest.fn().mockReturnValue({ name: "FunctionToString" }), + breadcrumbsIntegration: jest.fn().mockReturnValue({ name: "Breadcrumbs" }), + httpContextIntegration: jest.fn().mockReturnValue({ name: "HttpContext" }), + dedupeIntegration: jest.fn().mockReturnValue({ name: "Dedupe" }), + rewriteFramesIntegration: jest.fn().mockReturnValue({ name: "RewriteFrames" }), +})); + +describe("initSentry", () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + it("does nothing if no sentry config is given", async () => { + await initSentry(undefined); + expect(Sentry.init).not.toHaveBeenCalled(); + }); + + it("normalizes Element Desktop's vector:// stack frames so they group with Element Web", async () => { + await initSentry({ + dsn: "https://examplePublicKey@o0.ingest.sentry.io/0", + environment: "test", + }); + + expect(Sentry.rewriteFramesIntegration).toHaveBeenCalledWith({ + root: "vector://vector/webapp", + prefix: "app://", + }); + + const { integrations } = jest.mocked(Sentry.init).mock.calls[0][0]!; + expect(integrations).toEqual(expect.arrayContaining([{ name: "RewriteFrames" }])); + }); +}); + +describe("rewriteFramesIntegration output", () => { + // Uses the real Sentry SDK integration (not the mock above) to verify the actual rewritten + // path, since it's easy to get the exact prefix/slash count wrong (e.g. `app:///` here would + // double up with the leading slash already present in the frame, producing `app:////...`). + // Relies on this test file's jsdom environment providing a real `window`, which is what makes + // the SDK's browser root/prefix substitution apply in the first place. + it("rewrites a vector:// frame to a clean app:/// path", async () => { + const RealSentry = jest.requireActual("@sentry/browser"); + + const integration = RealSentry.rewriteFramesIntegration({ + root: "vector://vector/webapp", + prefix: "app://", + }); + + const event: any = { + exception: { + values: [ + { + stacktrace: { + frames: [ + { + filename: "vector://vector/webapp/bundles/abc123/bundle.js", + }, + ], + }, + }, + ], + }, + }; + + const processed = integration.processEvent!(event, {}, {} as any) as any; + expect(processed.exception.values[0].stacktrace.frames[0].filename).toBe("app:///bundles/abc123/bundle.js"); + }); +});