From 3f98e36c560a876bdd05f65ecf415c68221b95f0 Mon Sep 17 00:00:00 2001 From: "Timo K." Date: Tue, 13 Oct 2026 17:22:48 +0200 Subject: [PATCH] test MatrixRTCError conversion --- src/utils/errors.test.ts | 133 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/src/utils/errors.test.ts b/src/utils/errors.test.ts index 306aa3cf8..e7aea3730 100644 --- a/src/utils/errors.test.ts +++ b/src/utils/errors.test.ts @@ -6,14 +6,37 @@ Please see LICENSE in the repository root for full details. */ import { describe, expect, test } from "vitest"; +import { ConnectionError } from "livekit-client"; +import { + ErrorCode as RTCErrorCode, + FailToGetOpenIdToken as RTCFailToGetOpenIdToken, + FailToStartLivekitConnection as RTCFailToStartLivekitConnection, + InsufficientCapacityError as RTCInsufficientCapacityError, + LivekitConnectionError as RTCLivekitConnectionError, + MatrixRTCError, + MatrixRTCTransportMissingError as RTCTransportMissingError, + MembershipManagerError as RTCMembershipManagerError, + NoMatrix2AuthorizationService as RTCNoMatrix2AuthorizationService, + PeerConnectionTimeoutError as RTCPeerConnectionTimeoutError, + SFURoomCreationRestrictedError as RTCSFURoomCreationRestrictedError, + UnknownRTCError, +} from "@element-hq/matrixrtc-sdk"; import { + ConnectionLostError, ErrorCategory, ErrorCode, + FailToGetOpenIdToken, FailToStartLivekitConnection, + fromMatrixRTCError, + InsufficientCapacityError, + LivekitConnectionError, + MatrixRTCTransportMissingError, MembershipManagerError, NoMatrix2AuthorizationService, + PeerConnectionTimeoutError, SFURoomCreationRestrictedError, + UnknownCallError, } from "./errors"; // These errors take their wording from Element Call's own i18next instance @@ -60,3 +83,113 @@ describe("localised errors", () => { expect(error.localisedMessage).not.toContain("error."); }); }); + +// The SDK reports failures as its own error classes; the app turns each into +// the translated error it shows, so every class the SDK can throw needs a +// counterpart with a title and no raw i18n key leaking through. +describe("fromMatrixRTCError", () => { + const cause = new Error("the underlying problem"); + + test.each([ + { + name: "missing transport", + input: new RTCTransportMissingError("example.org"), + expected: MatrixRTCTransportMissingError, + code: ErrorCode.MISSING_MATRIX_RTC_TRANSPORT, + }, + { + name: "membership manager failure", + input: new RTCMembershipManagerError(cause), + expected: MembershipManagerError, + code: ErrorCode.INTERNAL_MEMBERSHIP_MANAGER, + }, + { + name: "OpenID token failure", + input: new RTCFailToGetOpenIdToken(cause), + expected: FailToGetOpenIdToken, + code: ErrorCode.OPEN_ID_ERROR, + }, + { + name: "missing authorization service", + input: new RTCNoMatrix2AuthorizationService(cause), + expected: NoMatrix2AuthorizationService, + code: ErrorCode.NO_MATRIX_2_AUTHORIZATION_SERVICE, + }, + { + name: "LiveKit start failure", + input: new RTCFailToStartLivekitConnection("could not publish"), + expected: FailToStartLivekitConnection, + code: ErrorCode.FAILED_TO_START_LIVEKIT, + }, + { + name: "insufficient capacity", + input: new RTCInsufficientCapacityError(), + expected: InsufficientCapacityError, + code: ErrorCode.INSUFFICIENT_CAPACITY_ERROR, + }, + { + name: "restricted room creation", + input: new RTCSFURoomCreationRestrictedError(), + expected: SFURoomCreationRestrictedError, + code: ErrorCode.SFU_ERROR, + }, + { + name: "peer connection timeout", + input: new RTCPeerConnectionTimeoutError(), + expected: PeerConnectionTimeoutError, + code: ErrorCode.SFU_ERROR, + }, + { + name: "LiveKit connection failure", + input: new RTCLivekitConnectionError( + ConnectionError.timeout("signalling timed out"), + ), + expected: LivekitConnectionError, + code: ErrorCode.SFU_ERROR, + }, + { + name: "lost connection", + input: new MatrixRTCError("gone", RTCErrorCode.CONNECTION_LOST_ERROR), + expected: ConnectionLostError, + code: ErrorCode.CONNECTION_LOST_ERROR, + }, + { + name: "anything else", + input: new UnknownRTCError(cause), + expected: UnknownCallError, + code: ErrorCode.UNKNOWN_ERROR, + }, + ])("maps a $name to the app's error", ({ input, expected, code }) => { + const error = fromMatrixRTCError(input); + + expect(error).toBeInstanceOf(expected); + expect(error.code).toBe(code); + expect(error.localisedTitle).not.toContain("error."); + expect(error.localisedMessage ?? "").not.toContain("error."); + }); + + test("keeps the SDK error's cause for reporting", () => { + const error = fromMatrixRTCError(new RTCMembershipManagerError(cause)); + + expect(error.cause).toBe(cause); + }); + + test("keeps the SDK error itself as the cause when it has none", () => { + const input = new MatrixRTCError("no cause attached"); + const error = fromMatrixRTCError(input); + + expect(error).toBeInstanceOf(UnknownCallError); + expect(error.cause).toBe(input); + }); + + test("keeps the LiveKit reason in the connection error's description", () => { + const error = fromMatrixRTCError( + new RTCLivekitConnectionError(ConnectionError.timeout("timed out")), + ); + + expect(error.localisedMessageKey).toBe( + "error.livekit_connection_error_description", + ); + expect(error.localisedMessageValues?.reason).toBe("Timeout"); + }); +});