From fcdd8c93f4d77aeb17cdf541e01eb4e09be8a058 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 1 Nov 2022 09:01:52 +0000 Subject: [PATCH] [Backport staging] Catch server versions API call exception when starting the client (#2832) Co-authored-by: Germain --- spec/integ/matrix-client-syncing.spec.ts | 10 ++++++++ src/client.ts | 29 ++++++++++++++++-------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/spec/integ/matrix-client-syncing.spec.ts b/spec/integ/matrix-client-syncing.spec.ts index 9eab95077..a75af86ae 100644 --- a/spec/integ/matrix-client-syncing.spec.ts +++ b/spec/integ/matrix-client-syncing.spec.ts @@ -274,6 +274,16 @@ describe("MatrixClient syncing", () => { expect(fires).toBe(1); }); + + it("should work when all network calls fail", async () => { + httpBackend!.expectedRequests = []; + httpBackend!.when("GET", "").fail(0, new Error("CORS or something")); + const prom = client!.startClient(); + await Promise.all([ + expect(prom).resolves.toBeUndefined(), + httpBackend!.flushAllExpected(), + ]); + }); }); describe("initial sync", () => { diff --git a/src/client.ts b/src/client.ts index fdf39deff..1193978c2 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1192,15 +1192,17 @@ export class MatrixClient extends TypedEventEmitter} The server /versions response */ - public getVersions(): Promise { + public async getVersions(): Promise { if (this.serverVersionsPromise) { return this.serverVersionsPromise; } @@ -6530,13 +6532,20 @@ export class MatrixClient extends TypedEventEmitter { + ).catch(e => { // Need to unset this if it fails, otherwise we'll never retry this.serverVersionsPromise = null; // but rethrow the exception to anything that was waiting throw e; }); + const serverVersions = await this.serverVersionsPromise; + this.canSupport = await buildFeatureSupportMap(serverVersions); + + // We can set flag values to use their stable or unstable version + const support = this.canSupport.get(Feature.ThreadUnreadNotifications); + UNREAD_THREAD_NOTIFICATIONS.setPreferUnstable(support === ServerSupport.Unstable); + return this.serverVersionsPromise; }